美文网首页
LeetCode 76-80

LeetCode 76-80

作者: 1nvad3r | 来源:发表于2020-10-04 17:02 被阅读0次

76. Minimum Window Substring

分析:

以后再做。

Java:

77. Combinations

分析:

使用回溯+剪枝。

Java:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();

    void dfs(int index, int n, int k) {
        if (temp.size() == k) {
            res.add(new ArrayList<>(temp));
            return;
        }
        for (int i = index; i <= n; i++) {
            temp.add(i);
            dfs(i + 1, n, k);
            temp.remove(temp.size() - 1);
        }
    }

    public List<List<Integer>> combine(int n, int k) {
        dfs(1, n, k);
        return res;
    }
}

78. Subsets

分析:

回溯。使用index代表当前要访问的数组元素的下标。
当index等于数组长度时,结束递归。
回溯的过程中可以选第index号元素,也可以不选第index号元素。

Java:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();

    public void dfs(int index, int[] nums) {
        if (index == nums.length) {
            res.add(new ArrayList<>(temp));
            return;
        }
        temp.add(nums[index]);
        dfs(index + 1, nums);
        temp.remove(temp.size() - 1);
        dfs(index + 1, nums);
    }

    public List<List<Integer>> subsets(int[] nums) {
        dfs(0, nums);
        return res;
    }
}

79. Word Search

分析:

给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
使用深度优先遍历。在遍历过程中判断字符串是否存在,如果不存在直接返回false。

Java:
class Solution {

    boolean[][] isVisit;
    int[] X = {0, 0, -1, 1};//上下左右
    int[] Y = {-1, 1, 0, 0};

    //对所有规则进行判断
    boolean check(char[][] board, int i, int j, int k, String word) {
        if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) {
            return false;
        }
        if (isVisit[i][j] == true) {
            return false;
        }
        if (board[i][j] == word.charAt(k)) {
            return true;
        } else {
            return false;
        }
    }


    boolean dfs(char[][] board, int i, int j, int k, String word) {
        if (k == word.length()) {
            return true;
        }
        if (check(board, i, j, k, word) == true) {
            isVisit[i][j] = true;
            for (int z = 0; z < 4; z++) {
                int newI = i + X[z];
                int newJ = j + Y[z];
                if (dfs(board, newI, newJ, k + 1, word) == true) {
                    return true;
                }
            }
            //如果某一点四个方向都不满足条件,改回false
            isVisit[i][j] = false;
        } else {
            return false;
        }
        return false;
    }

    public boolean exist(char[][] board, String word) {
        isVisit = new boolean[board.length][board[0].length];
        //从每一个字母开始出发进行DFS
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                boolean res = dfs(board, i, j, 0, word);
                if (res == true) {
                    return true;
                } else {
                    continue;
                }
            }
        }
        return false;
    }
}

80. Remove Duplicates from Sorted Array II

分析:

使用双指针,i为数组中要访问的元素位置,j为下一个要覆盖的元素的位置。使用count记录重复的个数,count值小于等于2时,覆盖掉j位置的元素。

Java:
class Solution {
    public int removeDuplicates(int[] nums) {
        int j = 1, count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1]) {
                count++;
            } else {
                count = 1;
            }
            if (count <= 2) {
                nums[j++] = nums[i];
            }
        }
        return j;
    }
}

相关文章

  • LeetCode 76-80

    76. Minimum Window Substring[https://leetcode-cn.com/prob...

  • 76-80

    76 情侣们说:“萤火虫真是美丽啊!” 旁边草丛里的蜗牛仿佛听到了这世界上最恐怖的一句话。 77 人们问道:“为什...

  • 76-80

    v学习目录: 076讲:影响行为|给脸色的领导最没用 077讲:前馈管理|让下属听懂你的话 078讲:同理心|最重...

  • 诗篇76-80

    诗篇 第76篇 〔亚萨的诗歌,交与伶长。用丝弦的乐器。〕 在犹大,上帝为人所认识;在以色列,他的名为大。在撒冷,有...

  • 2019-08-11商学院

    时间:20190811晚7.30 地点:公司办公室 参与人:王首,周海刚,周银坤,陈辛龙林志明,邢珍 76-80章...

  • 诗词原创 观唐绝句76-80 春草风中鹧鸪语 似人空守越王台

    前言 继续发观唐绝句,76-80,其中第八十篇咏王昭君有两首,依旧例前边是老街习作,后面是唐诗人原作。 观唐绝句7...

  • week 2019-06-23

    LeetCode 16[M] LeetCode 17[M] LeetCode 926

  • leetcode

    leetcode leetcode to practice leetcode ac code 数组专题 done ...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

网友评论

      本文标题:LeetCode 76-80

      本文链接:https://www.haomeiwen.com/subject/crfnuktx.html