|
| 1 | +### <a href="https://leetcode-cn.com/problems/climbing-stairs/">爬楼梯</a> |
| 2 | +```java |
| 3 | +/** |
| 4 | + * 递归 |
| 5 | + */ |
| 6 | +public int climbStairs(int n) { |
| 7 | + return fibonacci(n, 1, 1); |
| 8 | +} |
| 9 | + |
| 10 | +public int fibonacci(int n, int a, int b) { |
| 11 | + return n <= 1 ? b : fibonacci(n - 1, b, a + b); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * 循环累加 f(n) = f(n - 1) + f(n - 2); |
| 16 | + */ |
| 17 | +public int climbStairs(int n) { |
| 18 | + int first = 1, second = 1; |
| 19 | + for (int i = 1; i < n; i++) { |
| 20 | + int temp = second; |
| 21 | + second += first; |
| 22 | + first = temp; |
| 23 | + } |
| 24 | + return second; |
| 25 | +} |
| 26 | +``` |
| 27 | +<br/> |
| 28 | + |
| 29 | +### <a href="https://leetcode-cn.com/problems/generate-parentheses/">括号生成</a> |
| 30 | +```java |
| 31 | +List<String> result = new ArrayList<>(); |
| 32 | +public List<String> generateParenthesis(int n) { |
| 33 | + recurse(n, n, ""); |
| 34 | + return result; |
| 35 | +} |
| 36 | + |
| 37 | +public void recurse(int left, int right, String str) { |
| 38 | + if (left == 0 && right == 0) { |
| 39 | + result.add(str); |
| 40 | + return; |
| 41 | + } |
| 42 | + // add "(" |
| 43 | + if (left > 0) recurse(left - 1, right, str + "("); |
| 44 | + // add ")" |
| 45 | + if (right > left) recurse(left, right - 1, str + ")"); |
| 46 | +} |
| 47 | +``` |
| 48 | +<br/> |
| 49 | + |
| 50 | +### <a href="https://leetcode-cn.com/problems/invert-binary-tree/description/">翻转二叉树</a> |
| 51 | +```java |
| 52 | +public TreeNode invertTree(TreeNode root) { |
| 53 | + recurse(root); |
| 54 | + return root; |
| 55 | +} |
| 56 | +public void recurse(TreeNode node) { |
| 57 | + if (node == null) return; |
| 58 | + // 翻转左右子节点 |
| 59 | + TreeNode left = node.left; |
| 60 | + node.left = node.right; |
| 61 | + node.right = left; |
| 62 | + // 遍历左子树 |
| 63 | + recurse(node.left); |
| 64 | + // 遍历右子树 |
| 65 | + recurse(node.right); |
| 66 | +} |
| 67 | +``` |
| 68 | +<br/> |
| 69 | + |
| 70 | +### <a href="https://leetcode-cn.com/problems/validate-binary-search-tree">验证二叉搜索树</a> |
| 71 | +```java |
| 72 | +public boolean isValidBST(TreeNode root) { |
| 73 | + return recurse(root, null, null); |
| 74 | +} |
| 75 | + |
| 76 | +public boolean recurse(TreeNode node, Integer lowwer, Integer upper) { |
| 77 | + if (node == null) return true; |
| 78 | + int val = node.val; |
| 79 | + /* |
| 80 | + * 根节点的左子树中: |
| 81 | + * 右子节点大于父节点且小于最近节点是父节点左节点的值 |
| 82 | + * 左子节点小于父节点 |
| 83 | + * 根节点的右子树中: |
| 84 | + * 左子节点小于父节点且大于最近节点是父节点右节点的值 |
| 85 | + * 右子节点大于父节点 |
| 86 | + */ |
| 87 | + if (lowwer != null && val <= lowwer) return false; |
| 88 | + if (upper != null && val >= upper) return false; |
| 89 | + return recurse(node.left, lowwer, val) && recurse(node.right, val, upper); |
| 90 | +} |
| 91 | +``` |
| 92 | +<br/> |
| 93 | + |
| 94 | +### <a href="https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/">二叉树的最大深度</a> |
| 95 | +```java |
| 96 | +int max; |
| 97 | + |
| 98 | +public int maxDepth(TreeNode root) { |
| 99 | + recurse(root, 0); |
| 100 | + return max; |
| 101 | +} |
| 102 | + |
| 103 | +public void recurse(TreeNode node, int curDepth) { |
| 104 | + if (node == null) { |
| 105 | + max = Math.max(max, curDepth); |
| 106 | + return; |
| 107 | + } |
| 108 | + // left child |
| 109 | + recurse(node.left, curDepth + 1); |
| 110 | + // right child |
| 111 | + recurse(node.right, curDepth + 1); |
| 112 | +} |
| 113 | +``` |
| 114 | +<br/> |
| 115 | + |
| 116 | +### <a href="https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/">二叉树的最小深度</a> |
| 117 | +```java |
| 118 | +public int minDepth(TreeNode root) { |
| 119 | + if (root == null) return 0; |
| 120 | + // left child |
| 121 | + int s1 = minDepth(root.left); |
| 122 | + // right child |
| 123 | + int s2 = minDepth(root.right); |
| 124 | + /* |
| 125 | + * 根节点到最小子节点的距离 |
| 126 | + * 无左节点时,从右节点中找,反之亦然 |
| 127 | + */ |
| 128 | + return root.left == null || root.right == null ? s1 + s2 + 1 : Math.min(s1, s2) + 1; |
| 129 | +} |
| 130 | +``` |
| 131 | +<br/> |
| 132 | + |
| 133 | +### <a href="https://leetcode-cn.com/problems/powx-n/">Pow(x, n)</a> |
| 134 | +```java |
| 135 | +public double myPow(double x, int n) { |
| 136 | + return n > 0 ? recurse(x, n) : 1 / recurse(x, -n); |
| 137 | +} |
| 138 | +public double recurse(double x, int n) { |
| 139 | + if (n == 0) return 1; |
| 140 | + double result = recurse(x, n / 2); |
| 141 | + return n % 2 == 0 ? result * result : result * result * x; |
| 142 | +} |
| 143 | +``` |
| 144 | +<br/> |
| 145 | + |
| 146 | +### <a href="https://leetcode-cn.com/problems/subsets/">子集</a> |
| 147 | +```java |
| 148 | +/** |
| 149 | + * 前序遍历 |
| 150 | + */ |
| 151 | +List<List<Integer>> result = new ArrayList<>(); |
| 152 | +public List<List<Integer>> subsets(int[] nums) { |
| 153 | + result.add(new ArrayList<>()); |
| 154 | + recurse(nums, 0, new ArrayList<>()); |
| 155 | + return result; |
| 156 | +} |
| 157 | + |
| 158 | +public void recurse(int[] nums, int index, List<Integer> subSet) { |
| 159 | + if (index >= nums.length) { |
| 160 | + return; |
| 161 | + } |
| 162 | + subSet = new ArrayList<>(subSet); |
| 163 | + result.add(subSet); |
| 164 | + recurse(nums, index + 1, subSet); |
| 165 | + subSet.add(nums[index]); |
| 166 | + recurse(nums, index + 1, subSet); |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * 回溯 |
| 171 | + */ |
| 172 | +List<List<Integer>> result = new ArrayList<>(); |
| 173 | +public List<List<Integer>> subsets(int[] nums) { |
| 174 | + result.add(new ArrayList<>()); |
| 175 | + recurse(nums, 0, new ArrayList<>()); |
| 176 | + return result; |
| 177 | +} |
| 178 | + |
| 179 | +public void recurse(int[] nums, int index, List<Integer> subSet) { |
| 180 | + if (index >= nums.length) { |
| 181 | + return; |
| 182 | + } |
| 183 | + subSet.add(nums[index]); |
| 184 | + result.add(new ArrayList<>(subSet)); |
| 185 | + recurse(nums, index + 1, subSet); |
| 186 | + subSet.remove(subSet.size() - 1); |
| 187 | + recurse(nums, index + 1, subSet); |
| 188 | +} |
| 189 | +``` |
| 190 | +<br/> |
| 191 | + |
| 192 | +### <a href="https://leetcode-cn.com/problems/majority-element/">多数元素</a> |
| 193 | +```java |
| 194 | +public int majorityElement(int[] nums) { |
| 195 | + int flag = nums[0], count = 1; |
| 196 | + for (int i = 1; i < nums.length; i++) { |
| 197 | + if (count == 0) flag = nums[i]; |
| 198 | + count += nums[i] == flag ? 1 : -1; |
| 199 | + } |
| 200 | + return flag; |
| 201 | +} |
| 202 | +``` |
| 203 | +<br/> |
| 204 | + |
| 205 | +### <a href="https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/">电话号码的字母组合</a> |
| 206 | +```java |
| 207 | +List<String> result = new ArrayList<>(); |
| 208 | +Map<Character, String[]> map = new HashMap<>(); |
| 209 | +{ |
| 210 | + map.put('2', new String[]{"a", "b", "c"}); |
| 211 | + map.put('3', new String[]{"d", "e", "f"}); |
| 212 | + map.put('4', new String[]{"g", "h", "i"}); |
| 213 | + map.put('5', new String[]{"j", "k", "l"}); |
| 214 | + map.put('6', new String[]{"m", "n", "o"}); |
| 215 | + map.put('7', new String[]{"p", "q", "r", "s"}); |
| 216 | + map.put('8', new String[]{"t", "u", "v"}); |
| 217 | + map.put('9', new String[]{"w", "x", "y", "z"}); |
| 218 | +} |
| 219 | + |
| 220 | +public List<String> letterCombinations(String digits) { |
| 221 | + if (digits == null || digits.length() == 0) return result; |
| 222 | + recurse(digits, 0, ""); |
| 223 | + return result; |
| 224 | +} |
| 225 | + |
| 226 | +public void recurse(String digits, int index, String str) { |
| 227 | + if (index >= digits.length()) { |
| 228 | + result.add(str); |
| 229 | + return; |
| 230 | + } |
| 231 | + for (String s : map.get(digits.charAt(index))) { |
| 232 | + recurse(digits, index + 1, str + s); |
| 233 | + } |
| 234 | +} |
| 235 | +``` |
| 236 | +<br/> |
| 237 | + |
| 238 | +### <a href="https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/">二叉树的最近公共祖先</a> |
| 239 | +```java |
| 240 | +public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 241 | + if (root == null || root == p || root == q) return root; |
| 242 | + TreeNode leftNode = lowestCommonAncestor(root.left, p, q); |
| 243 | + TreeNode rightNode = lowestCommonAncestor(root.right, p, q); |
| 244 | + return leftNode != null && rightNode != null ? root : (leftNode == null ? rightNode : leftNode); |
| 245 | +} |
| 246 | +``` |
| 247 | +<br/> |
| 248 | + |
| 249 | +### <a href="https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">从前序与中序遍历序列构造二叉树</a> |
| 250 | +```java |
| 251 | +Map<Integer, Integer> indexMap = new HashMap<>(); |
| 252 | +public TreeNode buildTree(int[] preorder, int[] inorder) { |
| 253 | + for (int i = 0; i < inorder.length; i++) { |
| 254 | + indexMap.put(inorder[i], i); |
| 255 | + } |
| 256 | + return recurse(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); |
| 257 | +} |
| 258 | + |
| 259 | +public TreeNode recurse(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) { |
| 260 | + if (preStart > preEnd) return null; |
| 261 | + // 获取根节点在inorder中的位置 |
| 262 | + int rootVal = preorder[preStart]; |
| 263 | + // 创建根节点 |
| 264 | + TreeNode root = new TreeNode(rootVal); |
| 265 | + int pIndex = indexMap.get(rootVal); |
| 266 | + // 左子树 |
| 267 | + root.left = recurse(preorder, preStart + 1, pIndex - inStart + preStart, inorder, inStart, pIndex - 1); |
| 268 | + // 右子树 |
| 269 | + root.right = recurse(preorder, preEnd + pIndex - inEnd + 1, preEnd, inorder, pIndex + 1, inEnd); |
| 270 | + return root; |
| 271 | +} |
| 272 | +``` |
| 273 | +<br/> |
| 274 | + |
| 275 | +### <a href="https://leetcode-cn.com/problems/combinations/">组合</a> |
| 276 | +```java |
| 277 | +List<List<Integer>> result = new ArrayList<>(); |
| 278 | +public List<List<Integer>> combine(int n, int k) { |
| 279 | + if (n < k) throw new RuntimeException("Incorrect input data."); |
| 280 | + recurse(n, 1, k, new ArrayList<>()); |
| 281 | + return result; |
| 282 | +} |
| 283 | + |
| 284 | +public void recurse(int n, int cur, int k, List<Integer> list) { |
| 285 | + if (list.size() == k) { |
| 286 | + result.add(list); |
| 287 | + return; |
| 288 | + } |
| 289 | + list.add(cur); |
| 290 | + if (cur <= n) recurse(n, cur + 1, k, new ArrayList<>(list)); |
| 291 | + if (n - cur + list.size() - 1 >= k) { |
| 292 | + // 回溯 |
| 293 | + list.remove(list.size() - 1); |
| 294 | + recurse(n, cur + 1, k, new ArrayList<>(list)); |
| 295 | + } |
| 296 | +} |
| 297 | +``` |
| 298 | +<br/> |
| 299 | + |
| 300 | +### <a href="https://leetcode-cn.com/problems/permutations/submissions/">全排列</a> |
| 301 | +```java |
| 302 | +List<List<Integer>> result = new ArrayList<>(); |
| 303 | +public List<List<Integer>> permute(int[] nums) { |
| 304 | + recurse(nums, 0); |
| 305 | + return result; |
| 306 | +} |
| 307 | + |
| 308 | +public void recurse(int[] nums, int index) { |
| 309 | + if (index == nums.length - 1) { |
| 310 | + List<Integer> list = new ArrayList<>(); |
| 311 | + for (Integer num : nums) { |
| 312 | + list.add(num); |
| 313 | + } |
| 314 | + result.add(list); |
| 315 | + return; |
| 316 | + } |
| 317 | + for (int i = index; i< nums.length; i++) { |
| 318 | + swap(nums, i, index); |
| 319 | + recurse(nums, index + 1); |
| 320 | + swap(nums, i, index); |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +private void swap(int[] nums, int i, int j) { |
| 325 | + // 交换数值 |
| 326 | + int temp = nums[i]; |
| 327 | + nums[i] = nums[j]; |
| 328 | + nums[j] = temp; |
| 329 | +} |
| 330 | +``` |
0 commit comments