美文网首页
LeetCode 126-130

LeetCode 126-130

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

126. 单词接龙 II

先使用bfs记录每个结点的前驱,然后使用dfs获得所有最短路径。

class Solution {
    Map<String, Set<String>> pre = new HashMap<>();//存放每个单词的前驱
    List<List<String>> res = new ArrayList<>();//最终结果
    List<String> temp = new ArrayList<>();//临时路径

    public void dfs(String endWord, String beginWord) {
        if (endWord.equals(beginWord)) {
            Collections.reverse(temp);
            res.add(new ArrayList<>(temp));
            Collections.reverse(temp);//还得转回来,不然会影响后面的结果
            return;
        }
        Set<String> set = pre.get(endWord);
        for (String s : set) {
            temp.add(s);
            dfs(s, beginWord);
            temp.remove(temp.size() - 1);
        }
    }

    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        Set<String> set = new HashSet<>(wordList);//使用set加快搜索
        if (set.isEmpty() || !set.contains(endWord)) {
            return new ArrayList<>();
        }
        Queue<String> q = new LinkedList<>();
        Set<String> visited = new HashSet<>();
        q.offer(beginWord);
        visited.add(beginWord);
        set.add(beginWord);
        boolean finish = false;
        while (!q.isEmpty()) {
            int size = q.size();
            Set<String> s = new HashSet<>();//关键点,每一层遍历完之后才能把这一层加到已访问中
            for (int i = 0; i < size; i++) {
                String front = q.poll();
                char[] array = front.toCharArray();
                for (int j = 0; j < array.length; j++) {
                    char temp = array[j];
                    for (char k = 'a'; k <= 'z'; k++) {
                        array[j] = k;
                        String word = String.valueOf(array);
                        if (word.equals(endWord)) {
                            finish = true;
                        }
                        if (!visited.contains(word) && set.contains(word)) {
                            if (pre.containsKey(word)) {
                                Set<String> se = pre.get(word);
                                se.add(front);
                                pre.put(word, se);
                            } else {
                                Set<String> se = new HashSet<>();
                                se.add(front);
                                pre.put(word, se);
                            }
                            s.add(word);
                            q.add(word);
                        }
                        array[j] = temp;
                    }
                }
            }
            visited.addAll(s);
            if (finish == true) {
                break;
            }
        }
        if (pre.isEmpty()) {
            return new ArrayList<>();
        }
        temp.add(endWord);
        dfs(endWord, beginWord);
        return res;
    }
}

127. 单词接龙

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> set = new HashSet<>(wordList);
        if (set.size() == 0 || !set.contains(endWord)) {
            return 0;
        }
        set.remove(beginWord);
        Queue<String> q = new LinkedList<>();
        q.offer(beginWord);
        Set<String> visited = new HashSet<>();
        visited.add(beginWord);
        int step = 1;
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                String front = q.poll();
                char[] charArray = front.toCharArray();
                for (int j = 0; j < charArray.length; j++) {
                    char temp = charArray[j];
                    for (char k = 'a'; k <= 'z'; k++) {
                        if (k == charArray[j]) {
                            continue;
                        }
                        charArray[j] = k;
                        String word = String.valueOf(charArray);
                        if (word.equals(endWord)) {
                            return step + 1;
                        }
                        if (set.contains(word) && !visited.contains(word)) {
                            q.add(word);
                            visited.add(word);
                        }
                    }
                    charArray[j] = temp;
                }
            }
            step++;
        }
        return 0;
    }
}

128. 最长连续序列

class Solution {
    int[] father, size;
    int res = 1;

    public void init() {
        for (int i = 0; i < father.length; i++) {
            father[i] = i;
            size[i] = 1;
        }
    }
    //没有写路径压缩
    public int findFather(int x) {
        while (father[x] != x) {
            x = father[x];
        }
        return x;
    }

    public void merge(int a, int b) {
        int faA = findFather(a), faB = findFather(b);
        if (faA != faB) {
            father[faA] = faB;
            size[faB] += size[faA];
            res = Math.max(res, size[faB]);
        }
    }

    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        father = new int[nums.length];
        size = new int[nums.length];
        init();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (Integer key : map.keySet()) {
            if (map.containsKey(key + 1)) {
                merge(map.get(key), map.get(key + 1));
            }
        }
        return res;
    }
}

129. 求根到叶子节点数字之和

class Solution {
    int res = 0;

    public void dfs(TreeNode root, int num) {
        if (root == null) {
            return;
        }
        num *= 10;
        num += root.val;
        if (root.left == null && root.right == null) {
            res += num;
        }
        dfs(root.left, num);
        dfs(root.right, num);
    }

    public int sumNumbers(TreeNode root) {
        dfs(root, 0);
        return res;
    }
}
class Solution {
    public int sumNumbers(TreeNode root) {
        return helper(root, 0);
    }

    public int helper(TreeNode root, int i) {
        if (root == null) return 0;
        int temp = i * 10 + root.val;
        if (root.left == null && root.right == null)
            return temp;
        return helper(root.left, temp) + helper(root.right, temp);
    }
}

130. 被围绕的区域

首先对边界上每一个'O'做深度优先搜索,将与其相连的所有'O'改为'-'。然后遍历矩阵,将矩阵中所有'O'改为'X',将矩阵中所有'-'变为'O'

class Solution {
    int row, col;

    public void dfs(char[][] board, int i, int j) {
        if (i < 0 || j < 0 || i >= row || j >= col || board[i][j] != 'O') {
            return;
        }
        board[i][j] = '-';
        dfs(board, i - 1, j);
        dfs(board, i + 1, j);
        dfs(board, i, j - 1);
        dfs(board, i, j + 1);
    }

    public void solve(char[][] board) {
        if (board == null || board.length == 0) {
            return;
        }
        row = board.length;
        col = board[0].length;
        for (int i = 0; i < row; i++) {
            dfs(board, i, 0);
            dfs(board, i, col - 1);
        }
        for (int j = 0; j < col; j++) {
            dfs(board, 0, j);
            dfs(board, row - 1, j);
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
                if (board[i][j] == '-') {
                    board[i][j] = 'O';
                }
            }
        }
    }
}

相关文章

  • LeetCode 126-130

    126. 单词接龙 II[https://leetcode-cn.com/problems/word-ladder...

  • 126-130

    有一个好的导师真的是非常重要的事情,虽然不是专业技能的训练,但我在每一个阶段都遇到了,不论自己还是他人都想跟随的老...

  • 诗词126-130

    20150920 126 西江月·世事一场大梦 苏轼世事一场大梦,人生几度秋凉?夜来风叶已鸣廊。看取眉头鬓上。酒贱...

  • 诗篇126-130

    诗篇 第126篇 〔上行之诗。〕 当耶和华将那些被掳的带回锡安的时候,我们好像作梦的人。我们满口喜笑、满舌欢呼的时...

  • 微胖女孩夏季穿搭Tips

    Hi,女孩~ 本人身高164cm,近3个月体重在126-130斤之间浮动,晚餐吃多130斤,晚餐少吃126斤,半年...

  • 主角-第126-130章

    晨读打卡《主角》 阅读篇章:第126-130章 书籍内容: 石怀玉绝对是个好画家,好书法家,好艺术家。他的作品也的...

  • 1000 lucky moments | 126-130

    Jun 10th. 1.早上到天河公园给敏姐女儿拍了几张儿童照,小孩子真的很调皮,很难拍,不过又很可爱,满是生命的...

  • 英语口语 126-130

    1. heaven forbid 但愿不会发生这事 Heaven/ God forbid + that ... 但...

  • week 2019-06-23

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

  • leetcode

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

网友评论

      本文标题:LeetCode 126-130

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