深度优先搜索和广度优先搜索广泛运用于树和图中,但是它们的应用远远不止如此。

BFS


Leetcode 题解 - 搜索 - 图1

广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。

第一层:

  • 0 -> {6,2,1,5}

第二层:

  • 6 -> {4}
  • 2 -> {}
  • 1 -> {}
  • 5 -> {3}

第三层:

  • 4 -> {}
  • 3 -> {}

每一层遍历的节点都与根节点距离相同。设 d 表示第 i 个节点与根节点的距离,推导出一个结论:对于先遍历的节点 i 与后遍历的节点 j,有 d <= d。利用这个结论,可以求解最短路径等 最优解 问题:第一次遍历到目的节点,其所经过的路径为最短路径。应该注意的是,使用 BFS 只能求解无权图的最短路径,无权图是指从一个节点到另一个节点的代价都记为 1。

在程序实现 BFS 时需要考虑以下问题:

  • 队列:用来存储每一轮遍历得到的节点;
  • 标记:对于遍历过的节点,应该将它标记,防止重复遍历。

1. 计算在网格中从原点到特定点的最短路径长度

  1. Shortest Path in Binary Matrix(Medium)

Leetcode / 力扣

  1. [[1,1,0,1],
  2. [1,0,1,0],
  3. [1,1,1,1],
  4. [1,0,1,1]]

题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。

  1. public int shortestPathBinaryMatrix(int[][] grids) {
  2. if (grids == null || grids.length == 0 || grids[0].length == 0) {
  3. return -1;
  4. }
  5. int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
  6. int m = grids.length, n = grids[0].length;
  7. Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
  8. queue.add(new Pair<>(0, 0));
  9. int pathLength = 0;
  10. while (!queue.isEmpty()) {
  11. int size = queue.size();
  12. pathLength++;
  13. while (size-- > 0) {
  14. Pair<Integer, Integer> cur = queue.poll();
  15. int cr = cur.getKey(), cc = cur.getValue();
  16. if (grids[cr][cc] == 1) {
  17. continue;
  18. }
  19. if (cr == m - 1 && cc == n - 1) {
  20. return pathLength;
  21. }
  22. grids[cr][cc] = 1; // 标记
  23. for (int[] d : direction) {
  24. int nr = cr + d[0], nc = cc + d[1];
  25. if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
  26. continue;
  27. }
  28. queue.add(new Pair<>(nr, nc));
  29. }
  30. }
  31. }
  32. return -1;
  33. }

2. 组成整数的最小平方数数量

  1. Perfect Squares (Medium)

Leetcode / 力扣

  1. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

可以将每个整数看成图中的一个节点,如果两个整数之差为一个平方数,那么这两个整数所在的节点就有一条边。

要求解最小的平方数数量,就是求解从节点 n 到节点 0 的最短路径。

本题也可以用动态规划求解,在之后动态规划部分中会再次出现。

  1. public int numSquares(int n) {
  2. List<Integer> squares = generateSquares(n);
  3. Queue<Integer> queue = new LinkedList<>();
  4. boolean[] marked = new boolean[n + 1];
  5. queue.add(n);
  6. marked[n] = true;
  7. int level = 0;
  8. while (!queue.isEmpty()) {
  9. int size = queue.size();
  10. level++;
  11. while (size-- > 0) {
  12. int cur = queue.poll();
  13. for (int s : squares) {
  14. int next = cur - s;
  15. if (next < 0) {
  16. break;
  17. }
  18. if (next == 0) {
  19. return level;
  20. }
  21. if (marked[next]) {
  22. continue;
  23. }
  24. marked[next] = true;
  25. queue.add(next);
  26. }
  27. }
  28. }
  29. return n;
  30. }
  31. /**
  32. * 生成小于 n 的平方数序列
  33. * @return 1,4,9,...
  34. */
  35. private List<Integer> generateSquares(int n) {
  36. List<Integer> squares = new ArrayList<>();
  37. int square = 1;
  38. int diff = 3;
  39. while (square <= n) {
  40. squares.add(square);
  41. square += diff;
  42. diff += 2;
  43. }
  44. return squares;
  45. }

3. 最短单词路径

  1. Word Ladder (Medium)

Leetcode / 力扣

  1. Input:
  2. beginWord = "hit",
  3. endWord = "cog",
  4. wordList = ["hot","dot","dog","lot","log","cog"]
  5. Output: 5
  6. Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
  7. return its length 5.
  1. Input:
  2. beginWord = "hit"
  3. endWord = "cog"
  4. wordList = ["hot","dot","dog","lot","log"]
  5. Output: 0
  6. Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

题目描述:找出一条从 beginWord 到 endWord 的最短路径,每次移动规定为改变一个字符,并且改变之后的字符串必须在 wordList 中。

  1. public int ladderLength(String beginWord, String endWord, List<String> wordList) {
  2. wordList.add(beginWord);
  3. int N = wordList.size();
  4. int start = N - 1;
  5. int end = 0;
  6. while (end < N && !wordList.get(end).equals(endWord)) {
  7. end++;
  8. }
  9. if (end == N) {
  10. return 0;
  11. }
  12. List<Integer>[] graphic = buildGraphic(wordList);
  13. return getShortestPath(graphic, start, end);
  14. }
  15. private List<Integer>[] buildGraphic(List<String> wordList) {
  16. int N = wordList.size();
  17. List<Integer>[] graphic = new List[N];
  18. for (int i = 0; i < N; i++) {
  19. graphic[i] = new ArrayList<>();
  20. for (int j = 0; j < N; j++) {
  21. if (isConnect(wordList.get(i), wordList.get(j))) {
  22. graphic[i].add(j);
  23. }
  24. }
  25. }
  26. return graphic;
  27. }
  28. private boolean isConnect(String s1, String s2) {
  29. int diffCnt = 0;
  30. for (int i = 0; i < s1.length() && diffCnt <= 1; i++) {
  31. if (s1.charAt(i) != s2.charAt(i)) {
  32. diffCnt++;
  33. }
  34. }
  35. return diffCnt == 1;
  36. }
  37. private int getShortestPath(List<Integer>[] graphic, int start, int end) {
  38. Queue<Integer> queue = new LinkedList<>();
  39. boolean[] marked = new boolean[graphic.length];
  40. queue.add(start);
  41. marked[start] = true;
  42. int path = 1;
  43. while (!queue.isEmpty()) {
  44. int size = queue.size();
  45. path++;
  46. while (size-- > 0) {
  47. int cur = queue.poll();
  48. for (int next : graphic[cur]) {
  49. if (next == end) {
  50. return path;
  51. }
  52. if (marked[next]) {
  53. continue;
  54. }
  55. marked[next] = true;
  56. queue.add(next);
  57. }
  58. }
  59. }
  60. return 0;
  61. }

DFS


Leetcode 题解 - 搜索 - 图2

广度优先搜索一层一层遍历,每一层得到的所有新节点,要用队列存储起来以备下一层遍历的时候再遍历。

而深度优先搜索在得到一个新节点时立即对新节点进行遍历:从节点 0 出发开始遍历,得到到新节点 6 时,立马对新节点 6 进行遍历,得到新节点 4;如此反复以这种方式遍历新节点,直到没有新节点了,此时返回。返回到根节点 0 的情况是,继续对根节点 0 进行遍历,得到新节点 2,然后继续以上步骤。

从一个节点出发,使用 DFS 对一个图进行遍历时,能够遍历到的节点都是从初始节点可达的,DFS 常用来求解这种 可达性 问题。

在程序实现 DFS 时需要考虑以下问题:

  • 栈:用栈来保存当前节点信息,当遍历新节点返回时能够继续遍历当前节点。可以使用递归栈。
  • 标记:和 BFS 一样同样需要对已经遍历过的节点进行标记。

1. 查找最大的连通面积

  1. Max Area of Island (Medium)

Leetcode / 力扣

  1. [[0,0,1,0,0,0,0,1,0,0,0,0,0],
  2. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  3. [0,1,1,0,1,0,0,0,0,0,0,0,0],
  4. [0,1,0,0,1,1,0,0,1,0,1,0,0],
  5. [0,1,0,0,1,1,0,0,1,1,1,0,0],
  6. [0,0,0,0,0,0,0,0,0,0,1,0,0],
  7. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  8. [0,0,0,0,0,0,0,1,1,0,0,0,0]]
  1. private int m, n;
  2. private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  3. public int maxAreaOfIsland(int[][] grid) {
  4. if (grid == null || grid.length == 0) {
  5. return 0;
  6. }
  7. m = grid.length;
  8. n = grid[0].length;
  9. int maxArea = 0;
  10. for (int i = 0; i < m; i++) {
  11. for (int j = 0; j < n; j++) {
  12. maxArea = Math.max(maxArea, dfs(grid, i, j));
  13. }
  14. }
  15. return maxArea;
  16. }
  17. private int dfs(int[][] grid, int r, int c) {
  18. if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0) {
  19. return 0;
  20. }
  21. grid[r][c] = 0;
  22. int area = 1;
  23. for (int[] d : direction) {
  24. area += dfs(grid, r + d[0], c + d[1]);
  25. }
  26. return area;
  27. }

2. 矩阵中的连通分量数目

  1. Number of Islands (Medium)

Leetcode / 力扣

  1. Input:
  2. 11000
  3. 11000
  4. 00100
  5. 00011
  6. Output: 3

可以将矩阵表示看成一张有向图。

  1. private int m, n;
  2. private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  3. public int numIslands(char[][] grid) {
  4. if (grid == null || grid.length == 0) {
  5. return 0;
  6. }
  7. m = grid.length;
  8. n = grid[0].length;
  9. int islandsNum = 0;
  10. for (int i = 0; i < m; i++) {
  11. for (int j = 0; j < n; j++) {
  12. if (grid[i][j] != '0') {
  13. dfs(grid, i, j);
  14. islandsNum++;
  15. }
  16. }
  17. }
  18. return islandsNum;
  19. }
  20. private void dfs(char[][] grid, int i, int j) {
  21. if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') {
  22. return;
  23. }
  24. grid[i][j] = '0';
  25. for (int[] d : direction) {
  26. dfs(grid, i + d[0], j + d[1]);
  27. }
  28. }

3. 好友关系的连通分量数目

  1. Friend Circles (Medium)

Leetcode / 力扣

  1. Input:
  2. [[1,1,0],
  3. [1,1,0],
  4. [0,0,1]]
  5. Output: 2
  6. Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
  7. The 2nd student himself is in a friend circle. So return 2.

题目描述:好友关系可以看成是一个无向图,例如第 0 个人与第 1 个人是好友,那么 M[0][1] 和 M[1][0] 的值都为 1。

  1. private int n;
  2. public int findCircleNum(int[][] M) {
  3. n = M.length;
  4. int circleNum = 0;
  5. boolean[] hasVisited = new boolean[n];
  6. for (int i = 0; i < n; i++) {
  7. if (!hasVisited[i]) {
  8. dfs(M, i, hasVisited);
  9. circleNum++;
  10. }
  11. }
  12. return circleNum;
  13. }
  14. private void dfs(int[][] M, int i, boolean[] hasVisited) {
  15. hasVisited[i] = true;
  16. for (int k = 0; k < n; k++) {
  17. if (M[i][k] == 1 && !hasVisited[k]) {
  18. dfs(M, k, hasVisited);
  19. }
  20. }
  21. }

4. 填充封闭区域

  1. Surrounded Regions (Medium)

Leetcode / 力扣

  1. For example,
  2. X X X X
  3. X O O X
  4. X X O X
  5. X O X X
  6. After running your function, the board should be:
  7. X X X X
  8. X X X X
  9. X X X X
  10. X O X X

题目描述:使被 ‘X’ 包围的 ‘O’ 转换为 ‘X’。

先填充最外侧,剩下的就是里侧了。

  1. private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  2. private int m, n;
  3. public void solve(char[][] board) {
  4. if (board == null || board.length == 0) {
  5. return;
  6. }
  7. m = board.length;
  8. n = board[0].length;
  9. for (int i = 0; i < m; i++) {
  10. dfs(board, i, 0);
  11. dfs(board, i, n - 1);
  12. }
  13. for (int i = 0; i < n; i++) {
  14. dfs(board, 0, i);
  15. dfs(board, m - 1, i);
  16. }
  17. for (int i = 0; i < m; i++) {
  18. for (int j = 0; j < n; j++) {
  19. if (board[i][j] == 'T') {
  20. board[i][j] = 'O';
  21. } else if (board[i][j] == 'O') {
  22. board[i][j] = 'X';
  23. }
  24. }
  25. }
  26. }
  27. private void dfs(char[][] board, int r, int c) {
  28. if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] != 'O') {
  29. return;
  30. }
  31. board[r][c] = 'T';
  32. for (int[] d : direction) {
  33. dfs(board, r + d[0], c + d[1]);
  34. }
  35. }

5. 能到达的太平洋和大西洋的区域

  1. Pacific Atlantic Water Flow (Medium)

Leetcode / 力扣

  1. Given the following 5x5 matrix:
  2. Pacific ~ ~ ~ ~ ~
  3. ~ 1 2 2 3 (5) *
  4. ~ 3 2 3 (4) (4) *
  5. ~ 2 4 (5) 3 1 *
  6. ~ (6) (7) 1 4 5 *
  7. ~ (5) 1 1 2 4 *
  8. * * * * * Atlantic
  9. Return:
  10. [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

左边和上边是太平洋,右边和下边是大西洋,内部的数字代表海拔,海拔高的地方的水能够流到低的地方,求解水能够流到太平洋和大西洋的所有位置。

  1. private int m, n;
  2. private int[][] matrix;
  3. private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  4. public List<List<Integer>> pacificAtlantic(int[][] matrix) {
  5. List<List<Integer>> ret = new ArrayList<>();
  6. if (matrix == null || matrix.length == 0) {
  7. return ret;
  8. }
  9. m = matrix.length;
  10. n = matrix[0].length;
  11. this.matrix = matrix;
  12. boolean[][] canReachP = new boolean[m][n];
  13. boolean[][] canReachA = new boolean[m][n];
  14. for (int i = 0; i < m; i++) {
  15. dfs(i, 0, canReachP);
  16. dfs(i, n - 1, canReachA);
  17. }
  18. for (int i = 0; i < n; i++) {
  19. dfs(0, i, canReachP);
  20. dfs(m - 1, i, canReachA);
  21. }
  22. for (int i = 0; i < m; i++) {
  23. for (int j = 0; j < n; j++) {
  24. if (canReachP[i][j] && canReachA[i][j]) {
  25. ret.add(Arrays.asList(i, j));
  26. }
  27. }
  28. }
  29. return ret;
  30. }
  31. private void dfs(int r, int c, boolean[][] canReach) {
  32. if (canReach[r][c]) {
  33. return;
  34. }
  35. canReach[r][c] = true;
  36. for (int[] d : direction) {
  37. int nextR = d[0] + r;
  38. int nextC = d[1] + c;
  39. if (nextR < 0 || nextR >= m || nextC < 0 || nextC >= n
  40. || matrix[r][c] > matrix[nextR][nextC]) {
  41. continue;
  42. }
  43. dfs(nextR, nextC, canReach);
  44. }
  45. }

Backtracking


Backtracking(回溯)属于 DFS。

  • 普通 DFS 主要用在 可达性问题 ,这种问题只需要执行到特点的位置然后返回即可。
  • 而 Backtracking 主要用于求解 排列组合 问题,例如有 { ‘a’,’b’,’c’ } 三个字符,求解所有由这三个字符排列得到的字符串,这种问题在执行到特定的位置返回之后还会继续执行求解过程。

因为 Backtracking 不是立即返回,而要继续求解,因此在程序实现时,需要注意对元素的标记问题:

  • 在访问一个新元素进入新的递归调用时,需要将新元素标记为已经访问,这样才能在继续递归调用时不用重复访问该元素;
  • 但是在递归返回时,需要将元素标记为未访问,因为只需要保证在一个递归链中不同时访问一个元素,可以访问已经访问过但是不在当前递归链中的元素。

1. 数字键盘组合

  1. Letter Combinations of a Phone Number (Medium)

Leetcode / 力扣

Leetcode 题解 - 搜索 - 图3

  1. Input:Digit string "23"
  2. Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
  1. private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
  2. public List<String> letterCombinations(String digits) {
  3. List<String> combinations = new ArrayList<>();
  4. if (digits == null || digits.length() == 0) {
  5. return combinations;
  6. }
  7. doCombination(new StringBuilder(), combinations, digits);
  8. return combinations;
  9. }
  10. private void doCombination(StringBuilder prefix, List<String> combinations, final String digits) {
  11. if (prefix.length() == digits.length()) {
  12. combinations.add(prefix.toString());
  13. return;
  14. }
  15. int curDigits = digits.charAt(prefix.length()) - '0';
  16. String letters = KEYS[curDigits];
  17. for (char c : letters.toCharArray()) {
  18. prefix.append(c); // 添加
  19. doCombination(prefix, combinations, digits);
  20. prefix.deleteCharAt(prefix.length() - 1); // 删除
  21. }
  22. }

2. IP 地址划分

  1. Restore IP Addresses(Medium)

Leetcode / 力扣

  1. Given "25525511135",
  2. return ["255.255.11.135", "255.255.111.35"].
  1. public List<String> restoreIpAddresses(String s) {
  2. List<String> addresses = new ArrayList<>();
  3. StringBuilder tempAddress = new StringBuilder();
  4. doRestore(0, tempAddress, addresses, s);
  5. return addresses;
  6. }
  7. private void doRestore(int k, StringBuilder tempAddress, List<String> addresses, String s) {
  8. if (k == 4 || s.length() == 0) {
  9. if (k == 4 && s.length() == 0) {
  10. addresses.add(tempAddress.toString());
  11. }
  12. return;
  13. }
  14. for (int i = 0; i < s.length() && i <= 2; i++) {
  15. if (i != 0 && s.charAt(0) == '0') {
  16. break;
  17. }
  18. String part = s.substring(0, i + 1);
  19. if (Integer.valueOf(part) <= 255) {
  20. if (tempAddress.length() != 0) {
  21. part = "." + part;
  22. }
  23. tempAddress.append(part);
  24. doRestore(k + 1, tempAddress, addresses, s.substring(i + 1));
  25. tempAddress.delete(tempAddress.length() - part.length(), tempAddress.length());
  26. }
  27. }
  28. }

3. 在矩阵中寻找字符串

  1. Word Search (Medium)

Leetcode / 力扣

  1. For example,
  2. Given board =
  3. [
  4. ['A','B','C','E'],
  5. ['S','F','C','S'],
  6. ['A','D','E','E']
  7. ]
  8. word = "ABCCED", -> returns true,
  9. word = "SEE", -> returns true,
  10. word = "ABCB", -> returns false.
  1. private final static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  2. private int m;
  3. private int n;
  4. public boolean exist(char[][] board, String word) {
  5. if (word == null || word.length() == 0) {
  6. return true;
  7. }
  8. if (board == null || board.length == 0 || board[0].length == 0) {
  9. return false;
  10. }
  11. m = board.length;
  12. n = board[0].length;
  13. boolean[][] hasVisited = new boolean[m][n];
  14. for (int r = 0; r < m; r++) {
  15. for (int c = 0; c < n; c++) {
  16. if (backtracking(0, r, c, hasVisited, board, word)) {
  17. return true;
  18. }
  19. }
  20. }
  21. return false;
  22. }
  23. private boolean backtracking(int curLen, int r, int c, boolean[][] visited, final char[][] board, final String word) {
  24. if (curLen == word.length()) {
  25. return true;
  26. }
  27. if (r < 0 || r >= m || c < 0 || c >= n
  28. || board[r][c] != word.charAt(curLen) || visited[r][c]) {
  29. return false;
  30. }
  31. visited[r][c] = true;
  32. for (int[] d : direction) {
  33. if (backtracking(curLen + 1, r + d[0], c + d[1], visited, board, word)) {
  34. return true;
  35. }
  36. }
  37. visited[r][c] = false;
  38. return false;
  39. }

4. 输出二叉树中所有从根到叶子的路径

  1. Binary Tree Paths (Easy)

Leetcode / 力扣

  1. 1
  2. / \
  3. 2 3
  4. \
  5. 5
  1. ["1->2->5", "1->3"]
  1. public List<String> binaryTreePaths(TreeNode root) {
  2. List<String> paths = new ArrayList<>();
  3. if (root == null) {
  4. return paths;
  5. }
  6. List<Integer> values = new ArrayList<>();
  7. backtracking(root, values, paths);
  8. return paths;
  9. }
  10. private void backtracking(TreeNode node, List<Integer> values, List<String> paths) {
  11. if (node == null) {
  12. return;
  13. }
  14. values.add(node.val);
  15. if (isLeaf(node)) {
  16. paths.add(buildPath(values));
  17. } else {
  18. backtracking(node.left, values, paths);
  19. backtracking(node.right, values, paths);
  20. }
  21. values.remove(values.size() - 1);
  22. }
  23. private boolean isLeaf(TreeNode node) {
  24. return node.left == null && node.right == null;
  25. }
  26. private String buildPath(List<Integer> values) {
  27. StringBuilder str = new StringBuilder();
  28. for (int i = 0; i < values.size(); i++) {
  29. str.append(values.get(i));
  30. if (i != values.size() - 1) {
  31. str.append("->");
  32. }
  33. }
  34. return str.toString();
  35. }

5. 排列

  1. Permutations (Medium)

Leetcode / 力扣

  1. [1,2,3] have the following permutations:
  2. [
  3. [1,2,3],
  4. [1,3,2],
  5. [2,1,3],
  6. [2,3,1],
  7. [3,1,2],
  8. [3,2,1]
  9. ]
  1. public List<List<Integer>> permute(int[] nums) {
  2. List<List<Integer>> permutes = new ArrayList<>();
  3. List<Integer> permuteList = new ArrayList<>();
  4. boolean[] hasVisited = new boolean[nums.length];
  5. backtracking(permuteList, permutes, hasVisited, nums);
  6. return permutes;
  7. }
  8. private void backtracking(List<Integer> permuteList, List<List<Integer>> permutes, boolean[] visited, final int[] nums) {
  9. if (permuteList.size() == nums.length) {
  10. permutes.add(new ArrayList<>(permuteList)); // 重新构造一个 List
  11. return;
  12. }
  13. for (int i = 0; i < visited.length; i++) {
  14. if (visited[i]) {
  15. continue;
  16. }
  17. visited[i] = true;
  18. permuteList.add(nums[i]);
  19. backtracking(permuteList, permutes, visited, nums);
  20. permuteList.remove(permuteList.size() - 1);
  21. visited[i] = false;
  22. }
  23. }

6. 含有相同元素求排列

  1. Permutations II (Medium)

Leetcode / 力扣

  1. [1,1,2] have the following unique permutations:
  2. [[1,1,2], [1,2,1], [2,1,1]]

数组元素可能含有相同的元素,进行排列时就有可能出现重复的排列,要求重复的排列只返回一个。

在实现上,和 Permutations 不同的是要先排序,然后在添加一个元素时,判断这个元素是否等于前一个元素,如果等于,并且前一个元素还未访问,那么就跳过这个元素。

  1. public List<List<Integer>> permuteUnique(int[] nums) {
  2. List<List<Integer>> permutes = new ArrayList<>();
  3. List<Integer> permuteList = new ArrayList<>();
  4. Arrays.sort(nums); // 排序
  5. boolean[] hasVisited = new boolean[nums.length];
  6. backtracking(permuteList, permutes, hasVisited, nums);
  7. return permutes;
  8. }
  9. private void backtracking(List<Integer> permuteList, List<List<Integer>> permutes, boolean[] visited, final int[] nums) {
  10. if (permuteList.size() == nums.length) {
  11. permutes.add(new ArrayList<>(permuteList));
  12. return;
  13. }
  14. for (int i = 0; i < visited.length; i++) {
  15. if (i != 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
  16. continue; // 防止重复
  17. }
  18. if (visited[i]){
  19. continue;
  20. }
  21. visited[i] = true;
  22. permuteList.add(nums[i]);
  23. backtracking(permuteList, permutes, visited, nums);
  24. permuteList.remove(permuteList.size() - 1);
  25. visited[i] = false;
  26. }
  27. }

7. 组合

  1. Combinations (Medium)

Leetcode / 力扣

  1. If n = 4 and k = 2, a solution is:
  2. [
  3. [2,4],
  4. [3,4],
  5. [2,3],
  6. [1,2],
  7. [1,3],
  8. [1,4],
  9. ]
  1. public List<List<Integer>> combine(int n, int k) {
  2. List<List<Integer>> combinations = new ArrayList<>();
  3. List<Integer> combineList = new ArrayList<>();
  4. backtracking(combineList, combinations, 1, k, n);
  5. return combinations;
  6. }
  7. private void backtracking(List<Integer> combineList, List<List<Integer>> combinations, int start, int k, final int n) {
  8. if (k == 0) {
  9. combinations.add(new ArrayList<>(combineList));
  10. return;
  11. }
  12. for (int i = start; i <= n - k + 1; i++) { // 剪枝
  13. combineList.add(i);
  14. backtracking(combineList, combinations, i + 1, k - 1, n);
  15. combineList.remove(combineList.size() - 1);
  16. }
  17. }

8. 组合求和

  1. Combination Sum (Medium)

Leetcode / 力扣

  1. given candidate set [2, 3, 6, 7] and target 7,
  2. A solution set is:
  3. [[7],[2, 2, 3]]
  1. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  2. List<List<Integer>> combinations = new ArrayList<>();
  3. backtracking(new ArrayList<>(), combinations, 0, target, candidates);
  4. return combinations;
  5. }
  6. private void backtracking(List<Integer> tempCombination, List<List<Integer>> combinations,
  7. int start, int target, final int[] candidates) {
  8. if (target == 0) {
  9. combinations.add(new ArrayList<>(tempCombination));
  10. return;
  11. }
  12. for (int i = start; i < candidates.length; i++) {
  13. if (candidates[i] <= target) {
  14. tempCombination.add(candidates[i]);
  15. backtracking(tempCombination, combinations, i, target - candidates[i], candidates);
  16. tempCombination.remove(tempCombination.size() - 1);
  17. }
  18. }
  19. }

9. 含有相同元素的组合求和

  1. Combination Sum II (Medium)

Leetcode / 力扣

  1. For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
  2. A solution set is:
  3. [
  4. [1, 7],
  5. [1, 2, 5],
  6. [2, 6],
  7. [1, 1, 6]
  8. ]
  1. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  2. List<List<Integer>> combinations = new ArrayList<>();
  3. Arrays.sort(candidates);
  4. backtracking(new ArrayList<>(), combinations, new boolean[candidates.length], 0, target, candidates);
  5. return combinations;
  6. }
  7. private void backtracking(List<Integer> tempCombination, List<List<Integer>> combinations,
  8. boolean[] hasVisited, int start, int target, final int[] candidates) {
  9. if (target == 0) {
  10. combinations.add(new ArrayList<>(tempCombination));
  11. return;
  12. }
  13. for (int i = start; i < candidates.length; i++) {
  14. if (i != 0 && candidates[i] == candidates[i - 1] && !hasVisited[i - 1]) {
  15. continue;
  16. }
  17. if (candidates[i] <= target) {
  18. tempCombination.add(candidates[i]);
  19. hasVisited[i] = true;
  20. backtracking(tempCombination, combinations, hasVisited, i + 1, target - candidates[i], candidates);
  21. hasVisited[i] = false;
  22. tempCombination.remove(tempCombination.size() - 1);
  23. }
  24. }
  25. }

10. 1-9 数字的组合求和

  1. Combination Sum III (Medium)

Leetcode / 力扣

  1. Input: k = 3, n = 9
  2. Output:
  3. [[1,2,6], [1,3,5], [2,3,4]]

从 1-9 数字中选出 k 个数不重复的数,使得它们的和为 n。

  1. public List<List<Integer>> combinationSum3(int k, int n) {
  2. List<List<Integer>> combinations = new ArrayList<>();
  3. List<Integer> path = new ArrayList<>();
  4. backtracking(k, n, 1, path, combinations);
  5. return combinations;
  6. }
  7. private void backtracking(int k, int n, int start,
  8. List<Integer> tempCombination, List<List<Integer>> combinations) {
  9. if (k == 0 && n == 0) {
  10. combinations.add(new ArrayList<>(tempCombination));
  11. return;
  12. }
  13. if (k == 0 || n == 0) {
  14. return;
  15. }
  16. for (int i = start; i <= 9; i++) {
  17. tempCombination.add(i);
  18. backtracking(k - 1, n - i, i + 1, tempCombination, combinations);
  19. tempCombination.remove(tempCombination.size() - 1);
  20. }
  21. }

11. 子集

  1. Subsets (Medium)

Leetcode / 力扣

找出集合的所有子集,子集不能重复,[1, 2] 和 [2, 1] 这种子集算重复

  1. public List<List<Integer>> subsets(int[] nums) {
  2. List<List<Integer>> subsets = new ArrayList<>();
  3. List<Integer> tempSubset = new ArrayList<>();
  4. for (int size = 0; size <= nums.length; size++) {
  5. backtracking(0, tempSubset, subsets, size, nums); // 不同的子集大小
  6. }
  7. return subsets;
  8. }
  9. private void backtracking(int start, List<Integer> tempSubset, List<List<Integer>> subsets,
  10. final int size, final int[] nums) {
  11. if (tempSubset.size() == size) {
  12. subsets.add(new ArrayList<>(tempSubset));
  13. return;
  14. }
  15. for (int i = start; i < nums.length; i++) {
  16. tempSubset.add(nums[i]);
  17. backtracking(i + 1, tempSubset, subsets, size, nums);
  18. tempSubset.remove(tempSubset.size() - 1);
  19. }
  20. }

12. 含有相同元素求子集

  1. Subsets II (Medium)

Leetcode / 力扣

  1. For example,
  2. If nums = [1,2,2], a solution is:
  3. [
  4. [2],
  5. [1],
  6. [1,2,2],
  7. [2,2],
  8. [1,2],
  9. []
  10. ]
  1. public List<List<Integer>> subsetsWithDup(int[] nums) {
  2. Arrays.sort(nums);
  3. List<List<Integer>> subsets = new ArrayList<>();
  4. List<Integer> tempSubset = new ArrayList<>();
  5. boolean[] hasVisited = new boolean[nums.length];
  6. for (int size = 0; size <= nums.length; size++) {
  7. backtracking(0, tempSubset, subsets, hasVisited, size, nums); // 不同的子集大小
  8. }
  9. return subsets;
  10. }
  11. private void backtracking(int start, List<Integer> tempSubset, List<List<Integer>> subsets, boolean[] hasVisited,
  12. final int size, final int[] nums) {
  13. if (tempSubset.size() == size) {
  14. subsets.add(new ArrayList<>(tempSubset));
  15. return;
  16. }
  17. for (int i = start; i < nums.length; i++) {
  18. if (i != 0 && nums[i] == nums[i - 1] && !hasVisited[i - 1]) {
  19. continue;
  20. }
  21. tempSubset.add(nums[i]);
  22. hasVisited[i] = true;
  23. backtracking(i + 1, tempSubset, subsets, hasVisited, size, nums);
  24. hasVisited[i] = false;
  25. tempSubset.remove(tempSubset.size() - 1);
  26. }
  27. }

13. 分割字符串使得每个部分都是回文数

  1. Palindrome Partitioning (Medium)

Leetcode / 力扣

  1. For example, given s = "aab",
  2. Return
  3. [
  4. ["aa","b"],
  5. ["a","a","b"]
  6. ]
  1. public List<List<String>> partition(String s) {
  2. List<List<String>> partitions = new ArrayList<>();
  3. List<String> tempPartition = new ArrayList<>();
  4. doPartition(s, partitions, tempPartition);
  5. return partitions;
  6. }
  7. private void doPartition(String s, List<List<String>> partitions, List<String> tempPartition) {
  8. if (s.length() == 0) {
  9. partitions.add(new ArrayList<>(tempPartition));
  10. return;
  11. }
  12. for (int i = 0; i < s.length(); i++) {
  13. if (isPalindrome(s, 0, i)) {
  14. tempPartition.add(s.substring(0, i + 1));
  15. doPartition(s.substring(i + 1), partitions, tempPartition);
  16. tempPartition.remove(tempPartition.size() - 1);
  17. }
  18. }
  19. }
  20. private boolean isPalindrome(String s, int begin, int end) {
  21. while (begin < end) {
  22. if (s.charAt(begin++) != s.charAt(end--)) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. }

14. 数独

  1. Sudoku Solver (Hard)

Leetcode / 力扣

Leetcode 题解 - 搜索 - 图4

  1. private boolean[][] rowsUsed = new boolean[9][10];
  2. private boolean[][] colsUsed = new boolean[9][10];
  3. private boolean[][] cubesUsed = new boolean[9][10];
  4. private char[][] board;
  5. public void solveSudoku(char[][] board) {
  6. this.board = board;
  7. for (int i = 0; i < 9; i++)
  8. for (int j = 0; j < 9; j++) {
  9. if (board[i][j] == '.') {
  10. continue;
  11. }
  12. int num = board[i][j] - '0';
  13. rowsUsed[i][num] = true;
  14. colsUsed[j][num] = true;
  15. cubesUsed[cubeNum(i, j)][num] = true;
  16. }
  17. backtracking(0, 0);
  18. }
  19. private boolean backtracking(int row, int col) {
  20. while (row < 9 && board[row][col] != '.') {
  21. row = col == 8 ? row + 1 : row;
  22. col = col == 8 ? 0 : col + 1;
  23. }
  24. if (row == 9) {
  25. return true;
  26. }
  27. for (int num = 1; num <= 9; num++) {
  28. if (rowsUsed[row][num] || colsUsed[col][num] || cubesUsed[cubeNum(row, col)][num]) {
  29. continue;
  30. }
  31. rowsUsed[row][num] = colsUsed[col][num] = cubesUsed[cubeNum(row, col)][num] = true;
  32. board[row][col] = (char) (num + '0');
  33. if (backtracking(row, col)) {
  34. return true;
  35. }
  36. board[row][col] = '.';
  37. rowsUsed[row][num] = colsUsed[col][num] = cubesUsed[cubeNum(row, col)][num] = false;
  38. }
  39. return false;
  40. }
  41. private int cubeNum(int i, int j) {
  42. int r = i / 3;
  43. int c = j / 3;
  44. return r * 3 + c;
  45. }

15. N 皇后

  1. N-Queens (Hard)

Leetcode / 力扣

Leetcode 题解 - 搜索 - 图5

在 n*n 的矩阵中摆放 n 个皇后,并且每个皇后不能在同一行,同一列,同一对角线上,求所有的 n 皇后的解。

一行一行地摆放,在确定一行中的那个皇后应该摆在哪一列时,需要用三个标记数组来确定某一列是否合法,这三个标记数组分别为:列标记数组、45 度对角线标记数组和 135 度对角线标记数组。

45 度对角线标记数组的长度为 2 * n - 1,通过下图可以明确 (r, c) 的位置所在的数组下标为 r + c。

Leetcode 题解 - 搜索 - 图6

135 度对角线标记数组的长度也是 2 * n - 1,(r, c) 的位置所在的数组下标为 n - 1 - (r - c)。

Leetcode 题解 - 搜索 - 图7

  1. private List<List<String>> solutions;
  2. private char[][] nQueens;
  3. private boolean[] colUsed;
  4. private boolean[] diagonals45Used;
  5. private boolean[] diagonals135Used;
  6. private int n;
  7. public List<List<String>> solveNQueens(int n) {
  8. solutions = new ArrayList<>();
  9. nQueens = new char[n][n];
  10. for (int i = 0; i < n; i++) {
  11. Arrays.fill(nQueens[i], '.');
  12. }
  13. colUsed = new boolean[n];
  14. diagonals45Used = new boolean[2 * n - 1];
  15. diagonals135Used = new boolean[2 * n - 1];
  16. this.n = n;
  17. backtracking(0);
  18. return solutions;
  19. }
  20. private void backtracking(int row) {
  21. if (row == n) {
  22. List<String> list = new ArrayList<>();
  23. for (char[] chars : nQueens) {
  24. list.add(new String(chars));
  25. }
  26. solutions.add(list);
  27. return;
  28. }
  29. for (int col = 0; col < n; col++) {
  30. int diagonals45Idx = row + col;
  31. int diagonals135Idx = n - 1 - (row - col);
  32. if (colUsed[col] || diagonals45Used[diagonals45Idx] || diagonals135Used[diagonals135Idx]) {
  33. continue;
  34. }
  35. nQueens[row][col] = 'Q';
  36. colUsed[col] = diagonals45Used[diagonals45Idx] = diagonals135Used[diagonals135Idx] = true;
  37. backtracking(row + 1);
  38. colUsed[col] = diagonals45Used[diagonals45Idx] = diagonals135Used[diagonals135Idx] = false;
  39. nQueens[row][col] = '.';
  40. }
  41. }