前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >每日三题-子集、单词搜索、删除无效的括号

每日三题-子集、单词搜索、删除无效的括号

作者头像
才疏学浅的木子
发布2022-11-20 16:43:52
发布2022-11-20 16:43:52
54500
代码可运行
举报
文章被收录于专栏:CSDN文章CSDN文章
运行总次数:0
代码可运行

👨‍💻个人主页: 才疏学浅的木子 🙇‍♂️ 本人也在学习阶段如若发现问题,请告知非常感谢 🙇‍♂️ 📒 本文来自专栏: 算法 🌈 算法类型Hot100题 🌈 ❤️ 支持我:👍点赞 🌹收藏 🤟关注

每日三题

子集

解法一

递归+回溯

代码语言:javascript
代码运行次数:0
复制
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(res,list,nums,0);
        return res;
    }
    public void dfs(List<List<Integer>> res,List<Integer> list,int[] nums,int start){
        if(start > nums.length) return;
        else{
            res.add(new ArrayList(list));
        }
        for(int i = start;i < nums.length;i++){
            list.add(nums[i]);
            dfs(res,list,nums,i+1);
            list.remove(list.size()-1);
        } 
    }
}

单词搜索

解法一

递归+回溯

代码语言:javascript
代码运行次数:0
复制
class Solution {
    public boolean exist(char[][] board, String word) {
        boolean visited[][] = new boolean[board.length][board[0].length];
        for(int i = 0;i < board.length;i++){
            for(int j = 0;j < board[0].length;j++){
                if(dfs(board,word,0,visited,i,j)){
                    return true;
                }
            }
        }
        return false;
    }

    // idx 为需要找的word的下标
    // visited 为是否访问过
    // i,j为当前访问元素的行与列下标
    public boolean dfs(char[][] board,String word,int idx,boolean[][] visited,int i,int j){
        if(idx == word.length()-1 && board[i][j] == word.charAt(idx)) return true;
        if(i > board.length || j > board[0].length || i < 0 || j < 0) return false;
        int[][] dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};

        if(board[i][j] != word.charAt(idx)) return false;

        for(int[] dir:dirs){
            visited[i][j] = true;
            int newi = i + dir[0];
            int newj = j + dir[1];
            if(newi >=0 && newi < visited.length && newj >= 0 && newj < visited[0].length){
                if(!visited[newi][newj]){
                    if(dfs(board,word,idx+1,visited,newi,newj)){
                        return true;
                    }
                }
            } 
            visited[i][j] = false;
        }
        return false;
    }
}

删除无效的括号

代码语言:javascript
代码运行次数:0
复制
class Solution {
    public List<String> removeInvalidParentheses(String s) {
        int leftRemove = 0;
        int rightRemove = 0;

        // 找到左右括号删除最小的数量
        for(int i = 0;i < s.length();i++){
            if(s.charAt(i) == '('){
                leftRemove++;
            }
            if(s.charAt(i) == ')'){
                if(leftRemove == 0){
                    rightRemove++;
                }else{
                    leftRemove--;
                }
            }
        }

        List<String> res = new ArrayList<>();
        dfs(res,s,leftRemove,rightRemove,0);
        return res;
    }

    public void dfs(List<String> res,String str,int leftRemove,int rightRemove,int idx){
        if(leftRemove == 0 && rightRemove == 0){
            if(isYouXiaoKuoHao(str)){
                res.add(str);
            }
            return;
        }
        for(int i = idx;i < str.length();i++){
            if(i > 0 && str.charAt(i) == str.charAt(i-1)) continue;
            //删除左括号
            if(leftRemove > 0 && str.charAt(i) == '('){
                dfs(res,str.substring(0,i)+str.substring(i+1),leftRemove-1,rightRemove,i);
            }
            //删除右括号
            if(rightRemove > 0 && str.charAt(i) == ')'){
                dfs(res,str.substring(0,i)+str.substring(i+1),leftRemove,rightRemove-1,i);
            }
        }

    }

    public boolean isYouXiaoKuoHao(String s){
        int left = 0,right = 0;
        for(int i = 0;i < s.length();i++){
            if(s.charAt(i) == '('){
                left++;
            }
            if(s.charAt(i) == ')'){
                right++;
                if(right > left) return false;
            }
        }
        if(left != right) return false;
        return true;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 每日三题
  • 子集
  • 单词搜索
  • 删除无效的括号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档