JAVA刷题记录: 递归,搜索与回溯

专题一 递归

面试题 08.06. 汉诺塔问题 - 力扣(LeetCode)

class Solution {public void hanota(List<Integer> A, List<Integer> B, List<Integer> C) {dfs(A, B, C, A.size());}public void dfs(List<Integer> a, List<Integer> b, List<Integer> c, int num) {if(num == 1) {c.add(a.remove(a.size() - 1));return;}dfs(a, c, b, num - 1);c.add(a.remove(a.size() - 1));dfs(b, a, c, num - 1);return;}
}

21. 合并两个有序链表 - 力扣(LeetCode)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {if(list1 == null) return list2;if(list2 == null) return list1;if(list1.val <= list2.val) {list1.next = mergeTwoLists(list1.next, list2); return list1;}else {list2.next = mergeTwoLists(list1, list2.next); return list2;}}
}

206. 反转链表 - 力扣(LeetCode)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {if(head == null || head.next == null) return head;ListNode newHead = reverseList(head.next);head.next.next = head;head.next = null;return newHead;}
}

24. 两两交换链表中的节点 - 力扣(LeetCode)

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode swapPairs(ListNode head) {if(head == null || head.next == null) return head;ListNode tmp = swapPairs(head.next.next);ListNode ret = head.next;head.next = tmp;ret.next = head;return ret;}
}

专题二 二叉树中的深搜

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public boolean evaluateTree(TreeNode root) {if(root.left == null) return root.val == 0 ? false : true;boolean left = evaluateTree(root.left);boolean right = evaluateTree(root.right);return root.val == 2 ? left | right : left & right;}
}

129. 求根节点到叶节点数字之和 - 力扣(LeetCode)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public int sumNumbers(TreeNode root) {return sum(root, 0);}public int sum(TreeNode root, int pre) {pre = pre * 10 + root.val;if(root.left == null && root.right == null) return pre; int ret = 0;if(root.left != null) ret += sum(root.left, pre);if(root.right != null) ret += sum(root.right, pre);return ret; }
}

814. 二叉树剪枝 - 力扣(LeetCode)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode pruneTree(TreeNode root) {if(root == null) return null;root.left = pruneTree(root.left);root.right = pruneTree(root.right);if(root.left == null && root.right == null && root.val == 0) root = null;return root;}
}

98. 验证二叉搜索树 - 力扣(LeetCode)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {long pre = Long.MIN_VALUE;public boolean isValidBST(TreeNode root) {if(root == null)  return true;boolean left = isValidBST(root.left);if(left == false) return false;boolean cur = false;if(pre < root.val) cur = true;if(cur == false) return false;pre = root.val;boolean right = isValidBST(root.right);return right;}
}

230. 二叉搜索树中第 K 小的元素 - 力扣(LeetCode)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {int count;int ret;public int kthSmallest(TreeNode root, int k) {count = k; ret = 0;dfs(root);return ret;}public void dfs(TreeNode root) {if(root == null || count == 0) return ;dfs(root.left);count--;if(count == 0) {ret = root.val;return;}dfs(root.right);}
}

257. 二叉树的所有路径 - 力扣(LeetCode)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {List<String> ret;public List<String> binaryTreePaths(TreeNode root) {ret = new ArrayList<>();dfs(root, new StringBuffer());return ret;}public void dfs(TreeNode root, StringBuffer _path) {StringBuffer path = new StringBuffer(_path);path.append(Integer.toString(root.val));if(root.left == null && root.right == null) {ret.add(path.toString());return;}path.append("->");if(root.left != null) dfs(root.left, path);if(root.right != null) dfs(root.right, path);}
}

专题三 dfs

46. 全排列 - 力扣(LeetCode)

class Solution {List<List<Integer>> ret;List<Integer> path;boolean[] check;public List<List<Integer>> permute(int[] nums) {ret = new ArrayList<>();path = new ArrayList<>();check = new boolean[nums.length];dfs(nums);return ret;}public void dfs(int[] nums) {if(path.size() == nums.length) {ret.add(new ArrayList(path)); return ;}for(int i = 0; i < nums.length; i++) {if(check[i] == false) {check[i] = true;path.add(nums[i]);dfs(nums);check[i] = false;path.remove(path.size() - 1);}}}
}

78. 子集 - 力扣(LeetCode)

class Solution {List<List<Integer>> ret;List<Integer> path;public List<List<Integer>> subsets(int[] nums) {path = new ArrayList<>();ret = new ArrayList<>();dfs(nums, 0);return ret;}public void dfs(int[] nums, int i) {if(i == nums.length) {ret.add(new ArrayList(path));return ;}path.add(nums[i]);dfs(nums, i + 1);path.remove(path.size() - 1);dfs(nums, i + 1);}
}
class Solution {List<List<Integer>> ret;List<Integer> path;public List<List<Integer>> subsets(int[] nums) {path = new ArrayList<>();ret = new ArrayList<>();dfs(nums, 0);return ret;}public void dfs(int[] nums, int pos) {ret.add(new ArrayList(path));for(int i = pos; i < nums.length; i++) {path.add(nums[i]);dfs(nums, i + 1);path.remove(path.size() - 1);}}
}

专题四 综合练习

1863. 找出所有子集的异或总和再求和 - 力扣(LeetCode)

class Solution {int sum = 0;int path = 0;public int subsetXORSum(int[] nums) {dfs(nums, 0);return sum;}public void dfs(int[] nums, int pos) {sum += path;for(int i = pos; i < nums.length; i++) {path ^= nums[i];dfs(nums, i + 1);path ^= nums[i];}}
}

47. 全排列 II - 力扣(LeetCode)

class Solution {List<List<Integer>> ret;List<Integer> path;boolean[] check;public List<List<Integer>> permuteUnique(int[] nums) {ret = new ArrayList<>();path = new ArrayList<>();check = new boolean[nums.length];Arrays.sort(nums);dfs(nums, 0);return ret;}public void dfs(int[] nums, int pos) {if(pos == nums.length){ ret.add(new ArrayList(path)); return;}for(int i = 0; i < nums.length; i++) {if(check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false)) {continue;}check[i] = true;path.add(nums[i]);dfs(nums, pos + 1);check[i] = false;path.remove(path.size() - 1);}return;}
}

17. 电话号码的字母组合 - 力扣(LeetCode)

class Solution {String[] hash = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxzy"};List<String> ret;StringBuffer path;public List<String> letterCombinations(String digits) {path = new StringBuffer();ret = new ArrayList<>();if(digits.length() == 0) return ret;dfs(digits, 0);return ret;}public void dfs(String digits, int pos) {if(pos == digits.length()) {ret.add(path.toString());return;}String cur = hash[digits.charAt(pos) - '0'];for(int i = 0; i < cur.length(); i++) {path.append(cur.charAt(i));dfs(digits, pos + 1);path.deleteCharAt(path.length() - 1);}}
}

22. 括号生成 - 力扣(LeetCode)

class Solution {int left, right, n;List<String> ret;StringBuffer path;public List<String> generateParenthesis(int _n) {left = right = 0;n = _n;ret = new ArrayList<>();path = new StringBuffer();dfs();return ret;}public void dfs() {if(right == n) {ret.add(path.toString());}if(left < n) {left++;path.append('(');dfs();left--;path.deleteCharAt(path.length() - 1);}if(right < left) {right++;path.append(')');dfs();right--;path.deleteCharAt(path.length() - 1);}}
}

77. 组合 - 力扣(LeetCode)

class Solution {int n, k;List<List<Integer>> ret;List<Integer> path;public List<List<Integer>> combine(int _n, int _k) {n = _n; k = _k;ret = new ArrayList<>();path = new ArrayList<>();dfs(1);return ret;}public void dfs(int start) {if(path.size() == k) {ret.add(new ArrayList<>(path));return;}for(int i = start; i <= n; i++) {path.add(i);dfs(i + 1);path.remove(path.size() - 1);}}
}

494. 目标和 - 力扣(LeetCode)

class Solution {int ret, aim;public int findTargetSumWays(int[] nums, int target) {aim = target;ret = 0;dfs(nums, 0, 0);return ret;}public void dfs(int[] nums, int pos, int path) {if(pos == nums.length) {if(path == aim) ret++;return;}dfs(nums, pos + 1, path + nums[pos]);dfs(nums, pos + 1, path - nums[pos]);}
}

39. 组合总和 - 力扣(LeetCode)

class Solution {int aim;List<List<Integer>> ret;List<Integer> path;public List<List<Integer>> combinationSum(int[] candidates, int target) {aim = target;ret = new ArrayList<>();path = new ArrayList<>();dfs(candidates, 0, 0);return ret;}public void dfs(int[] nums, int pos, int sum) {if(sum == aim) {ret.add(new ArrayList(path));return;}if(sum > aim || pos == nums.length) return;for(int i = pos; i < nums.length; i++) {path.add(nums[i]);dfs(nums, i, sum + nums[i]);path.remove(path.size() - 1); }}
}

39. 组合总和 - 力扣(LeetCode)

class Solution {int aim;List<List<Integer>> ret;List<Integer> path;public List<List<Integer>> combinationSum(int[] candidates, int target) {aim = target;ret = new ArrayList<>();path = new ArrayList<>();dfs(candidates, 0, 0);return ret;}public void dfs(int[] nums, int pos, int sum) {if(sum == aim) {ret.add(new ArrayList(path));return;}if(sum > aim || pos == nums.length) return;for(int i = pos; i < nums.length; i++) {path.add(nums[i]);dfs(nums, i, sum + nums[i]);path.remove(path.size() - 1); }}
}

784. 字母大小写全排列 - 力扣(LeetCode)

class Solution {StringBuffer path;List<String> ret;public List<String> letterCasePermutation(String s) {path = new StringBuffer();ret = new ArrayList<>();dfs(s, 0);return ret;}public char change(char ch) {if(ch >= 'a' && ch <= 'z') return ch -= 32;else return ch += 32;}public void dfs(String s, int pos) {if(pos == s.length()) {ret.add(path.toString());return;}path.append(s.charAt(pos));dfs(s, pos + 1);path.deleteCharAt(path.length() - 1);if(s.charAt(pos) < '0' || s.charAt(pos) > '9'){path.append(change(s.charAt(pos)));dfs(s, pos + 1);path.deleteCharAt(path.length() - 1);}}
}

51. N 皇后 - 力扣(LeetCode)

class Solution {List<List<String>> ret;char[][] path;boolean[] checkCol, checkDig1, checkDig2;int n;public List<List<String>> solveNQueens(int _n) {n = _n;path = new char[n][n];ret = new ArrayList<>();checkCol = new boolean[n];checkDig1 = new boolean[2 * n];checkDig2 = new boolean[2 * n];for(int i = 0; i < n; i++) {Arrays.fill(path[i], '.');}dfs(0);return ret;}public void dfs(int row) {if(row == n) {List<String> tmp = new ArrayList<>();for(int i = 0; i < n; i++) {tmp.add(new String(path[i]));}ret.add(tmp);}for(int col = 0; col < n; col++) {if(checkCol[col] == false && checkDig1[row - col + n] == false && checkDig2[row + col] == false){path[row][col] = 'Q';checkCol[col] = checkDig1[row - col + n] = checkDig2[row + col] = true;dfs(row + 1);checkCol[col] = checkDig1[row - col + n] = checkDig2[row + col] = false;path[row][col] = '.';}}}
}

36. 有效的数独 - 力扣(LeetCode)

class Solution {boolean[][] row, col;boolean[][][] grid;public boolean isValidSudoku(char[][] board) {col = new boolean[9][10];row = new boolean[9][10];grid = new boolean[3][3][10];for(int i = 0; i < 9; i++) {for(int j = 0; j < 9; j++) {if(board[i][j] != '.'){int num = board[i][j] - '0';if(row[i][num] || col[j][num] || grid[i / 3][j / 3][num]){return false;}row[i][num] = col[j][num] = grid[i / 3][j / 3][num] = true;}}}return true;}
}

37. 解数独 - 力扣(LeetCode)

class Solution {boolean[][] row, col;boolean[][][] grid;public void solveSudoku(char[][] board) {col = new boolean[9][10];row = new boolean[9][10];grid = new boolean[3][3][10];for(int i = 0; i < 9; i++) {for(int j = 0; j < 9; j++) {if(board[i][j] != '.'){int num = board[i][j] - '0';row[i][num] = col[j][num] = grid[i / 3][j / 3][num] = true;}}}dfs(board);}public boolean dfs(char[][] board) {for(int i = 0; i < 9; i++) {for(int j = 0; j < 9; j++) {if(board[i][j] == '.') {for(int num = 1; num < 10; num++) {if(!row[i][num] && !col[j][num] && !grid[i / 3][j / 3][num]) {board[i][j] = (char)(num + '0');row[i][num] = col[j][num] = grid[i / 3][j / 3][num] = true;if(dfs(board) == true) return true;board[i][j] = '.';row[i][num] = col[j][num] = grid[i / 3][j / 3][num] = false;}}return false;}}}return true;}
}

79. 单词搜索 - 力扣(LeetCode)

class Solution {int m, n;char[] word;boolean[][] vis;public boolean exist(char[][] board, String _word) {m = board.length;n = board[0].length;vis = new boolean[m][n];word = _word.toCharArray();for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (board[i][j] == word[0]) {vis[i][j] = true;if (dfs(board, i, j, 1))return true;vis[i][j] = false;}}}return false;}int[] dx = { 0, 0, 1, -1 };int[] dy = { 1, -1, 0, 0 };public boolean dfs(char[][] board, int i, int j, int pos) {if (pos == word.length) {return true;}for (int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && board[x][y] == word[pos]) {vis[x][y] = true;if (dfs(board, x, y, pos + 1))return true;vis[x][y] = false;}}return false;}
}

1219. 黄金矿工 - 力扣(LeetCode)

class Solution {int ret, n, m;boolean[][] vis;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public int getMaximumGold(int[][] grid) {m = grid.length; n = grid[0].length;vis = new boolean[m][n];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(grid[i][j] != 0) {vis[i][j] = true;dfs(grid, i, j, grid[i][j]);vis[i][j] = false;}}}return ret;}public void dfs(int[][] grid, int i, int j, int path) {ret = Math.max(ret, path);for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] != 0) {vis[x][y] = true;dfs(grid, x, y, path + grid[x][y]);vis[x][y] = false;}}}
}

980. 不同路径 III - 力扣(LeetCode)

class Solution {int m, n, step, ret;boolean[][] vis;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public int uniquePathsIII(int[][] grid) {m = grid.length; n = grid[0].length;int bx = 0, by = 0;vis = new boolean[m][n];for(int i = 0; i < m; i++) {for(int j = 0;j < n; j++) {if(grid[i][j] == 0) step++;else if(grid[i][j] == 1) {bx = i; by = j;}else if(grid[i][j] == -1) vis[i][j] = true;}}step += 2;vis[bx][by] = true;dfs(grid, bx, by, 1);return ret;}public void dfs(int[][] grid, int i, int j, int count) {if(grid[i][j] == 2) {if(count == step) {ret++;}return;}for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {vis[x][y] = true;dfs(grid, x, y, count + 1);vis[x][y] = false;}}}
}

专题五 floodfill算法

733. 图像渲染 - 力扣(LeetCode)

class Solution {int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};int prev, m, n;public int[][] floodFill(int[][] image, int sr, int sc, int color) {if(image[sr][sc] == color) return image;prev = image[sr][sc];m = image.length;n = image[0].length;dfs(image, sr, sc, color);return image;     }public void dfs(int[][] image, int i, int j, int color) {image[i][j] = color;for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev) {dfs(image, x, y, color);}}}
}

200. 岛屿数量 - 力扣(LeetCode)

class Solution {int m, n, ret;boolean[][] vis;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public int numIslands(char[][] grid) {m = grid.length; n = grid[0].length;vis = new boolean[m][n];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(grid[i][j] == '1' && vis[i][j] == false) {ret++;dfs(grid, i, j);}   }}return ret;    }public void dfs(char[][] grid, int i, int j) {if(grid[i][j] == '0') return;grid[i][j] = '0';for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == '1') {dfs(grid, x, y);}}}
}

695. 岛屿的最大面积 - 力扣(LeetCode)

class Solution {int m, n, ret, count;boolean[][] vis;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public int maxAreaOfIsland(int[][] grid) {m = grid.length; n = grid[0].length;vis = new boolean[m][n];for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(grid[i][j] == 1 && !vis[i][j]) {count = 0;dfs(grid, i, j);ret = Math.max(ret, count);} }}    return ret;}public void dfs(int[][] grid, int i, int j) {if(grid[i][j] == 0) return;vis[i][j] = true;count++;for(int k = 0; k < 4; k++) {int x = i + dx[k];int y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && (grid[x][y] == 1) && (vis[x][y] == false)) {dfs(grid, x, y);}}}
}

130. 被围绕的区域 - 力扣(LeetCode)

class Solution {int m, n;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public void solve(char[][] board) {m = board.length;n = board[0].length;for(int i = 0; i < n; i++) {if(board[0][i] == 'O') dfs(board, 0, i);if(board[m - 1][i] == 'O') dfs(board, m - 1, i);}    for(int j = 0; j < m; j++) {if(board[j][0] == 'O') dfs(board, j, 0);if(board[j][n - 1] == 'O') dfs(board, j, n - 1);}for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(board[i][j] == '.') board[i][j] = 'O';else if(board[i][j] == 'O') board[i][j] = 'X';}}}public void dfs(char[][] board, int i, int j) {board[i][j] = '.';for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {dfs(board, x, y);}} }
}

417. 太平洋大西洋水流问题 - 力扣(LeetCode)

class Solution {int m, n;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public List<List<Integer>> pacificAtlantic(int[][] h) {m = h.length;n = h[0].length;boolean[][] pac = new boolean[m][n];boolean[][] atl = new boolean[m][n];for(int i = 0; i < m; i++) {dfs(h, i, 0, pac);dfs(h, i, n - 1, atl);}for(int j = 0; j < n; j++) {dfs(h, 0, j, pac);dfs(h, m - 1, j, atl);}List<List<Integer>> ret = new ArrayList<>();for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(pac[i][j] && atl[i][j]) {List<Integer> tmp = new ArrayList<>();tmp.add(i); tmp.add(j);ret.add(tmp);}}}return ret;}public void dfs(int[][] h, int i, int j, boolean[][] vis) {vis[i][j] = true;for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && h[x][y] >= h[i][j]) {dfs(h, x, y, vis);}}}
}

529. 扫雷游戏 - 力扣(LeetCode)

class Solution {int m, n;int[] dx = {0, 0, -1, 1, 1, 1, -1, -1};int[] dy = {-1, 1, 0, 0, 1, -1, 1, -1};public char[][] updateBoard(char[][] board, int[] click) {m = board.length; n = board[0].length;int x = click[0], y = click[1];if(board[x][y] == 'M') {board[x][y] = 'X';return board;}dfs(board, x, y);return board;}public void dfs(char[][] board, int i, int j) {int count = 0;for(int k = 0; k < 8; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') {count++;}}if(count == 0) {board[i][j] = 'B';for(int k = 0; k < 8; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E') {dfs(board, x, y);}}} else {board[i][j] = (char)(count + '0');}}
}

LCR 130. 衣橱整理 - 力扣(LeetCode)

class Solution {int m, n, k, ret;boolean[][] vis;int[] dx = {0, 1};int[] dy = {1, 0};public int wardrobeFinishing(int _m, int _n, int _k) {k = _k; m = _m; n = _n;vis = new boolean[m][n];dfs(0,0);return ret;}public boolean check(int i, int j) {int tmp = 0;while(i != 0) {tmp += i % 10;i /= 10;}while(j != 0) {tmp += j % 10;j /= 10;}return tmp <= k;}public void dfs(int i, int j) {ret++;vis[i][j] = true;for(int k = 0; k < 2; k++) {int x = i + dx[k], y = j + dy[k];if(x < m && y < n && check(x, y) && !vis[x][y]) {dfs(x, y);}}       }
}

专题六 记忆化搜索

62. 不同路径 - 力扣(LeetCode)

class Solution {int m, n;public int uniquePaths(int _m, int _n) {m = _m; n = _n;int[][] memo = new int[m + 1][n + 1];return dfs(m, n, memo);}public int dfs(int i, int j, int[][] memo) {if(memo[i][j] != 0) {return memo[i][j];}if(i == 0 || j == 0) {return 0;}if(i == 1 && j == 1) {memo[i][j] = 1;return 1;}memo[i][j] = dfs(i - 1, j, memo) + dfs(i, j - 1, memo);return memo[i][j];}
}

300. 最长递增子序列 - 力扣(LeetCode)

class Solution {int[] memo;int n, ret;public int lengthOfLIS(int[] nums) {n = nums.length;memo = new int[n];for(int i = 0; i < n; i++) {ret = Math.max(ret, dfs(nums, i));}return ret;}public int dfs(int[] nums, int pos) {if(memo[pos] != 0) return memo[pos];int ret = 1;for(int i = pos + 1; i < n; i++) {if(nums[i] > nums[pos]) {ret = Math.max(ret, dfs(nums, i) + 1);}}memo[pos] = ret;return ret;}
}

375. 猜数字大小 II - 力扣(LeetCode)

class Solution {int[][] memo;public int getMoneyAmount(int n) {memo = new int[n + 1][n + 1];return dfs(1, n);}public int dfs(int left, int right) {if(left >= right) return 0;if(memo[left][right] != 0) return memo[left][right];int ret = Integer.MAX_VALUE;for(int i = left; i <= right; i++) {int x = dfs(left, i - 1), y = dfs(i + 1, right);ret = Math.min(ret, Math.max(x, y) + i);}memo[left][right] = ret;return ret;}
}

329. 矩阵中的最长递增路径 - 力扣(LeetCode)

class Solution {int[][] memo;int m, n;int[] dx = {0, 0, -1, 1};int[] dy = {-1, 1, 0, 0};public int longestIncreasingPath(int[][] matrix) {m = matrix.length; n = matrix[0].length;memo = new int[m][n];int ret = 0;for(int i = 0; i < m;i++) {for(int j = 0; j < n; j++) {ret = Math.max(ret, dfs(matrix, i, j));}}   return ret;}public int dfs(int[][] matrix, int i, int j) {if(memo[i][j] != 0) return memo[i][j];int ret = 1;for(int k = 0; k < 4; k++) {int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) {ret = Math.max(ret, dfs(matrix, x, y) + 1);}}memo[i][j] = ret;return ret;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/79249.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

YOLOv11改进:利用RT-DETR主干网络PPHGNetV2助力轻量化目标检测

这里写自定义目录标题 YOLOv11改进&#xff1a;利用RT-DETR主干网络PPHGNetV2助力轻量化目标检测1. 介绍2. 引言3. 技术背景3.1 YOLOv11概述3.2 RT-DETR与PPHGNetV23.3 相关工作 4. 应用使用场景5. 详细代码实现5.1 环境准备5.2 PPHGNetV2主干网络实现5.3 YOLOv11与PPHGNetV2集…

WPF之Button控件详解

文章目录 1. 引言2. Button控件基础Button类定义 3. Button控件的核心属性3.1 Content属性3.2 IsDefault属性3.3 IsCancel属性3.4 其他常用属性 4. 按钮样式与模板自定义4.1 简单样式设置4.2 使用Style对象4.3 触发器使用4.4 使用ControlTemplate完全自定义4.5 按钮视觉状态 5.…

【Java】2025 年 Java 学习路线:从入门到精通

文章目录 一、Java基础阶段(4-8周)1. 开发环境搭建2. 核心语法基础3. 面向对象编程(OOP)4. 核心类库二、Java进阶阶段(6-10周)1. JVM深度理解2. 并发编程3. 新特性掌握4. 设计模式三、开发框架与中间件(8-12周)1. Spring生态2. 持久层框架3. 常用中间件四、项目实战阶段…

虚幻引擎入门笔记

【虚幻5】UE5新手入门尝试 虚幻引擎的基础设置 1.验证-当文件误删的时候&#xff0c;对其进行验证&#xff0c;可以恢复。 2.虚幻引擎极其强大&#xff0c;可以实现多种复合技能&#xff0c;所在创建项目页面可以看见不只是创建游戏的项目 3.更改虚幻引擎默认的缓存地址。有些…

【PostgreSQL数据分析实战:从数据清洗到可视化全流程】1.1 数据库核心概念与PostgreSQL技术优势

&#x1f449; 点击关注不迷路 &#x1f449; 点击关注不迷路 &#x1f449; 点击关注不迷路 文章大纲 深度解析PostgreSQL核心架构与技术优势&#xff1a;从数据库原理到实战场景1.1 数据库核心概念与PostgreSQL技术优势1.1.1 关系型数据库核心架构解析1.1.1.1 数据库系统的底…

详解SLAM中的李群和李代数(上)

1 概述 最近阅读高翔大神的《视觉SLAM十四讲》这本书&#xff0c;感觉整本书写的非常的平实&#xff0c;用非常接地气的语言毫无保留的介绍了视觉SLAM的相关知识&#xff0c;非常值得一读。不过&#xff0c;在第4章出现的李群和李代数的相关概念就有点令人难以费解了。其实这段…

libevent库详解:高性能异步IO的利器

目录 一、libevent 简介 主要特点&#xff1a; 二、事件模型原理 1. event_base 2. event 3. evconnlistener&#xff08;TCP监听器&#xff09; 4. bufferevent 简化流程如下&#xff1a; 三、libevent 使用示例 1. 创建事件主循环 2. 创建监听器&#xff08;TCP&a…

从 “零” 做个开源音乐软件“SteadyBeat”吧!<1> 准备

换换脑子&#xff0c;做个音乐软件&#xff0c;根据调性、和弦走向&#xff08;情感&#xff09;、节拍、速度等需求&#xff0c;结合AI和一众工具&#xff0c;自动生成伴奏、Solo等&#xff0c;有点像库乐队&#xff01;自己平时也用得着&#xff0c;暂时取名叫《SteadyBeat》…

npm error code CERT_HAS_EXPIRED

npm error code CERT_HAS_EXPIRED 欢迎来到我的主页&#xff0c;我是博主英杰&#xff0c;211科班出身&#xff0c;就职于医疗科技公司&#xff0c;热衷分享知识&#xff0c;武汉城市开发者社区主理人 擅长.net、C、python开发&#xff0c; 如果遇到技术问题&#xff0c;即可私…

数字世界的“私人车道“:网络切片如何用Python搭建专属通信高速路?

数字世界的"私人车道"&#xff1a;网络切片如何用Python搭建专属通信高速路&#xff1f; 2024年6月&#xff0c;中国移动宣布在浙江某智能工厂完成全球首个"5G工业网络切片"规模商用——这条为生产线定制的"数字专属车道"&#xff0c;将设备控制…

VSCode Verilog编辑仿真环境搭建

VSCode Verilog环境搭建 下载Iverilog安装Iverilog验证安装VS Code安装插件 下载Iverilog 官网下载Iverilog 安装Iverilog 一定要勾选这两项 建议勾选这两项 验证安装 运行Windows PowerShell输入命令&#xff1a;iverilog输入命令&#xff1a;Get-Command gtkwave …

C++ - 数据容器之 list(创建与初始化、元素访问、容量判断、元素遍历、添加元素、删除元素)

一、创建与初始化 引入 <list> 并使用 std 命名空间 #include <list>using namespace std;创建一个空 list list<int> my_list;创建一个包含 5 个元素&#xff0c;每个元素初始化为 0 的 list list<int> my_list(5);创建一个包含 5 个元素&#xf…

自动化测试项目1 --- 唠嗑星球 [软件测试实战 Java 篇]

目录 项目介绍 项目源码库地址 项目功能测试 1.自动化实施步骤 1.1 编写测试用例 1.2 自动化测试脚本开发 1.2.1 配置相关环境, 添加相关依赖 1.2.2 相关代码编写 2. 自动化功能测试总结 2.1 弹窗的解决相关问题 2.2 断言的使用和说明 2.3 重新登录问题 项目性能…

Codeforces Round 1022 (Div. 2)(ABC)

A. Permutation Warm-Up 翻译&#xff1a; 对于长度为 n 的排列 p&#xff0c;我们定义函数&#xff1a; 给你一个数 n。你需要计算函数 f(p) 在考虑从 1 到 n 的所有可能的数字排列时&#xff0c;可以取多少个不同的值。 思路&#xff1a; 按序排列时和为0&…

数据结构------C语言经典题目(6)

1.数据结构都学了些什么&#xff1f; 1.基本数据类型 算数类型&#xff1a; char&#xff08;字符&#xff09;、int&#xff08;整数&#xff09;、float&#xff08;单精度浮点数&#xff09;、double&#xff08;双精度浮点数&#xff09;等。 枚举类型&#xff1a; enum…

如何封装一个线程安全、可复用的 HBase 查询模板

目录 一、前言&#xff1a;原生 HBase 查询的痛点 &#xff08;一&#xff09;连接管理混乱&#xff0c;容易造成资源泄露 &#xff08;二&#xff09;查询逻辑重复&#xff0c;缺乏统一的模板 &#xff08;三&#xff09;多线程/高并发下的线程安全性隐患 &#xff08;四…

【中间件】bthread_基础_TaskControl

TaskControl 1 Definition2 Introduce**核心职责** 3 成员解析**3.1 数据结构与线程管理****3.2 任务调度与负载均衡****3.3 线程停放与唤醒&#xff08;ParkingLot&#xff09;****3.4 统计与监控** 4 **工作流程**5 **设计亮点**6 **使用场景示例**7 **总结**8 学习过程中的疑…

win11 终端 安装ffmpeg 使用终端Scoop

1、安装scoop (Windows 包管理器) Set-ExecutionPolicy RemoteSigned -Scope CurrentUser iwr -useb get.scoop.sh | iex 2、使用scoop来安装ffmpeg scoop install ffmpeg 3、测试一下ffmpeg&#xff0c;将Mp3文件转为Wav文件 ffmpeg -i A.mp3 A.wav 然后我们就看到A.wav生成…

力扣838.推多米诺随笔

“生活就像海洋&#xff0c;只有意志坚强的人&#xff0c;才能到达彼岸。”—— 马克思 题目 n 张多米诺骨牌排成一行&#xff0c;将每张多米诺骨牌垂直竖立。在开始时&#xff0c;同时把一些多米诺骨牌向左或向右推。 每过一秒&#xff0c;倒向左边的多米诺骨牌会推动其左侧…

超级好用的​​参数化3D CAD 建模​​图形库 (CadQuery库介绍)

CadQuery 库详细介绍​​ ​​CadQuery​​ 是一个基于 ​​Python​​ 的 ​​参数化 3D CAD 建模​​ 库&#xff0c;允许用户通过编写代码&#xff08;而不是传统 GUI&#xff09;来创建精确的 ​​3D 模型​​。它特别适用于 ​​自动化设计、机械工程、3D 打印​​ 等场景…