From ca9066fdffd5ba9b51907758bdab2285fcf82281 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Sat, 28 Apr 2018 14:14:33 +0800 Subject: [PATCH] auto commit --- "notes/Leetcode \351\242\230\350\247\243.md" | 340 +++++++++---------- 1 file changed, 170 insertions(+), 170 deletions(-) diff --git "a/notes/Leetcode \351\242\230\350\247\243.md" "b/notes/Leetcode \351\242\230\350\247\243.md" index 7dd8712d..e1b12f69 100644 --- "a/notes/Leetcode \351\242\230\350\247\243.md" +++ "b/notes/Leetcode \351\242\230\350\247\243.md" @@ -136,7 +136,7 @@ l m h **求开方** -[Leetcode : 69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/) +[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/) ```html Input: 4 @@ -172,7 +172,7 @@ public int mySqrt(int x) { **大于给定元素的最小元素** -[Leetcode : 744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/) +[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/) ```html Input: @@ -207,7 +207,7 @@ public char nextGreatestLetter(char[] letters, char target) { **有序数组的 Single Element** -[Leetcode : 540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) +[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) ```html Input: [1,1,2,3,3,4,4,8,8] @@ -240,7 +240,7 @@ public int singleNonDuplicate(int[] nums) { **第一个错误的版本** -[Leetcode : 278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/) +[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/) 题目描述:给定一个元素 n 代表有 [1, 2, ..., n] 版本,可以调用 isBadVersion(int x) 知道某个版本是否错误,要求找到第一个错误的版本。 @@ -264,7 +264,7 @@ public int firstBadVersion(int n) { **旋转数组的最小数字** -[Leetcode : 153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) +[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) ```html Input: [3,4,5,1,2], @@ -287,7 +287,7 @@ public int findMin(int[] nums) { **查找区间** -[Leetcode : 34. Search for a Range (Medium)](https://leetcode.com/problems/search-for-a-range/description/) +[34. Search for a Range (Medium)](https://leetcode.com/problems/search-for-a-range/description/) ```html Input: nums = [5,7,7,8,8,10], target = 8 @@ -326,7 +326,7 @@ private int binarySearch(int[] nums, int target) { **分配饼干** -[Leetcode : 455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/) +[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/) ```html Input: [1,2], [1,2,3] @@ -358,7 +358,7 @@ public int findContentChildren(int[] g, int[] s) { **股票的最大收益** -[Leetcode : 122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) +[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) 题目描述:一次交易包含买入和卖出,多个交易之间不能交叉进行。 @@ -378,7 +378,7 @@ public int maxProfit(int[] prices) { **种植花朵** -[Leetcode : 605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/) +[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/) ```html Input: flowerbed = [1,0,0,0,1], n = 1 @@ -405,7 +405,7 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) { **修改一个数成为非递减数组** -[Leetcode : 665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/) +[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/) ```html Input: [4,2,3] @@ -433,7 +433,7 @@ public boolean checkPossibility(int[] nums) { **判断是否为子串** -[Leetcode : 392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/) +[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/) ```html s = "abc", t = "ahbgdc" @@ -453,7 +453,7 @@ public boolean isSubsequence(String s, String t) { **投飞镖刺破气球** -[Leetcode : 452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/) +[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/) ``` Input: @@ -492,7 +492,7 @@ public int findMinArrowShots(int[][] points) { **分隔字符串使同种字符出现在一起** -[Leetcode : 763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/) +[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/) ```html Input: S = "ababcbacadefegdehijhklij" @@ -527,7 +527,7 @@ public List partitionLabels(String S) { **根据身高和序号重组队列** -[Leetcode : 406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/) +[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/) ```html Input: @@ -601,7 +601,7 @@ public int[] twoSum(int[] numbers, int target) { **两数平方和** -[Leetcode : 633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/) +[633. Sum of Square Numbers (Easy)](https://leetcode.com/problems/sum-of-square-numbers/description/) ```html Input: 5 @@ -626,7 +626,7 @@ public boolean judgeSquareSum(int c) { **反转字符串中的元音字符** -[Leetcode : 345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) +[345. Reverse Vowels of a String (Easy)](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) ```html Given s = "leetcode", return "leotcede". @@ -658,7 +658,7 @@ public String reverseVowels(String s) { **回文字符串** -[Leetcode : 680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/) +[680. Valid Palindrome II (Easy)](https://leetcode.com/problems/valid-palindrome-ii/description/) ```html Input: "abca" @@ -691,7 +691,7 @@ private boolean isPalindrome(String s, int i, int j) { **归并两个有序数组** -[Leetcode : 88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/) +[88. Merge Sorted Array (Easy)](https://leetcode.com/problems/merge-sorted-array/description/) ```html Input: @@ -719,7 +719,7 @@ public void merge(int[] nums1, int m, int[] nums2, int n) { **判断链表是否存在环** -[Leetcode : 141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/) +[141. Linked List Cycle (Easy)](https://leetcode.com/problems/linked-list-cycle/description/) 使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。 @@ -738,7 +738,7 @@ public boolean hasCycle(ListNode head) { **最长子序列** -[Leetcode : 524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/) +[524. Longest Word in Dictionary through Deleting (Medium)](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/) ``` Input: @@ -791,7 +791,7 @@ private boolean isValid(String s, String target) { **Kth Element** -[Leetocde : 215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) +[215. Kth Largest Element in an Array (Medium)](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) **排序** :时间复杂度 O(NlogN),空间复杂度 O(1) @@ -855,7 +855,7 @@ private void swap(int[] a, int i, int j) { **出现频率最多的 k 个数** -[Leetcode : 347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/) +[347. Top K Frequent Elements (Medium)](https://leetcode.com/problems/top-k-frequent-elements/description/) ```html Given [1,1,1,2,2,3] and k = 2, return [1,2]. @@ -890,7 +890,7 @@ public List topKFrequent(int[] nums, int k) { **按照字符出现次数对字符串排序** -[Leetcode : 451. Sort Characters By Frequency (Medium)](https://leetcode.com/problems/sort-characters-by-frequency/description/) +[451. Sort Characters By Frequency (Medium)](https://leetcode.com/problems/sort-characters-by-frequency/description/) ```html Input: @@ -1028,7 +1028,7 @@ private class Position { **查找最大的连通面积** -[Leetcode : 695. Max Area of Island (Easy)](https://leetcode.com/problems/max-area-of-island/description/) +[695. Max Area of Island (Easy)](https://leetcode.com/problems/max-area-of-island/description/) ```html [[0,0,1,0,0,0,0,1,0,0,0,0,0], @@ -1075,7 +1075,7 @@ private int dfs(int[][] grid, int r, int c) { **矩阵中的连通分量数目** -[Leetcode : 200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/) +[200. Number of Islands (Medium)](https://leetcode.com/problems/number-of-islands/description/) ```html 11110 @@ -1122,7 +1122,7 @@ private void dfs(char[][] grid, int i, int j) { **好友关系的连通分量数目** -[Leetcode : 547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/) +[547. Friend Circles (Medium)](https://leetcode.com/problems/friend-circles/description/) ```html Input: @@ -1165,7 +1165,7 @@ private void dfs(int[][] M, int i, boolean[] hasVisited) { **填充封闭区域** -[Leetcode : 130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/) +[130. Surrounded Regions (Medium)](https://leetcode.com/problems/surrounded-regions/description/) ```html For example, @@ -1220,7 +1220,7 @@ private void dfs(char[][] board, int r, int c) { **从两个方向都能到达的区域** -[Leetcode : 417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) +[417. Pacific Atlantic Water Flow (Medium)](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) ```html Given the following 5x5 matrix: @@ -1297,7 +1297,7 @@ Backtracking(回溯)属于 DFS。 **数字键盘组合** -[Leetcode : 17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) +[17. Letter Combinations of a Phone Number (Medium)](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)

@@ -1332,7 +1332,7 @@ private void combination(StringBuilder prefix, String digits, List ret) **IP 地址划分** -[Leetcode : 93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/) +[93. Restore IP Addresses(Medium)](https://leetcode.com/problems/restore-ip-addresses/description/) ```html Given "25525511135", @@ -1369,7 +1369,7 @@ private void doRestore(int k, StringBuilder path, String s, List address **在矩阵中寻找字符串** -[Leetcode : 79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/) +[79. Word Search (Medium)](https://leetcode.com/problems/word-search/description/) ```html For example, @@ -1423,7 +1423,7 @@ private boolean backtracking(char[][] board, boolean[][] visited, String word, i **输出二叉树中所有从根到叶子的路径** -[Leetcode : 257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/) +[257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths/description/) ```html 1 @@ -1476,7 +1476,7 @@ private String buildPath(List values) { **排列** -[Leetcode : 46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/) +[46. Permutations (Medium)](https://leetcode.com/problems/permutations/description/) ```html [1,2,3] have the following permutations: @@ -1517,7 +1517,7 @@ private void backtracking(List permuteList, boolean[] visited, int[] nu **含有相同元素求排列** -[Leetcode : 47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) +[47. Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) ```html [1,1,2] have the following unique permutations: @@ -1558,7 +1558,7 @@ private void backtracking(List permuteList, boolean[] visited, int[] nu **组合** -[Leetcode : 77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/) +[77. Combinations (Medium)](https://leetcode.com/problems/combinations/description/) ```html If n = 4 and k = 2, a solution is: @@ -1596,7 +1596,7 @@ private void backtracking(int start, int n, int k, List combineList, Li **组合求和** -[Leetcode : 39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/) +[39. Combination Sum (Medium)](https://leetcode.com/problems/combination-sum/description/) ```html given candidate set [2, 3, 6, 7] and target 7, @@ -1630,7 +1630,7 @@ A solution set is: **含有相同元素的求组合求和** -[Leetcode : 40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/) +[40. Combination Sum II (Medium)](https://leetcode.com/problems/combination-sum-ii/description/) ```html For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, @@ -1673,7 +1673,7 @@ private void doCombination(int[] candidates, int target, int start, List path, int start, List
@@ -1898,7 +1898,7 @@ private int cubeNum(int i, int j) { **N 皇后** -[Leetcode : 51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/) +[51. N-Queens (Hard)](https://leetcode.com/problems/n-queens/description/)

@@ -1963,7 +1963,7 @@ private void backstracking(int row) { **给表达式加括号** -[Leetcode : 241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) +[241. Different Ways to Add Parentheses (Medium)](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) ```html Input: "2-1-1". @@ -2015,7 +2015,7 @@ public List diffWaysToCompute(String input) { **爬楼梯** -[Leetcode : 70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/) +[70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/) 题目描述:有 N 阶楼梯,每次可以上一阶或者两阶,求有多少种上楼梯的方法。 @@ -2052,7 +2052,7 @@ public int climbStairs(int n) { **强盗抢劫** -[Leetcode : 198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/) +[198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/) 题目描述:抢劫一排住户,但是不能抢邻近的住户,求最大抢劫量。 @@ -2100,7 +2100,7 @@ public int rob(int[] nums) { **强盗在环形街区抢劫** -[Leetcode : 213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/) +[213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/) ```java private int[] dp; @@ -2159,7 +2159,7 @@ dp[N] 即为所求。 **最长递增子序列** -[Leetcode : 300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/) +[300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/) ```java public int lengthOfLIS(int[] nums) { @@ -2224,7 +2224,7 @@ private int binarySearch(int[] nums, int first, int last, int key) { **一组整数对能够构成的最长链** -[Leetcode : 646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) +[646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/) ```html Input: [[1,2], [2,3], [3,4]] @@ -2261,7 +2261,7 @@ public int findLongestChain(int[][] pairs) { **最长摆动子序列** -[Leetcode : 376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/) +[376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/) ```html Input: [1,7,4,9,2,5] @@ -2405,7 +2405,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) { **划分数组为和相等的两部分** -[Leetcode : 416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/) +[416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/) ```html Input: [1, 5, 11, 5] @@ -2438,7 +2438,7 @@ Explanation: The array can be partitioned as [1, 5, 5] and [11]. **字符串按单词列表分割** -[Leetcode : 139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/) +[139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/) ```html s = "leetcode", @@ -2469,7 +2469,7 @@ public boolean wordBreak(String s, List wordDict) { **改变一组数的正负号使得它们的和为一给定数** -[Leetcode : 494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/) +[494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/) ```html Input: nums is [1, 1, 1, 1, 1], S is 3. @@ -2531,7 +2531,7 @@ private int findTargetSumWays(int[] nums, int start, int S) { **01 字符构成最多的字符串** -[Leetcode : 474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/) +[474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/) ```html Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 @@ -2564,7 +2564,7 @@ public int findMaxForm(String[] strs, int m, int n) { **找零钱** -[Leetcode : 322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/) +[322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/) ```html Example 1: @@ -2599,7 +2599,7 @@ public int coinChange(int[] coins, int amount) { **组合总和** -[Leetcode : 377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/) +[377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/) ```html nums = [1, 2, 3] @@ -2639,7 +2639,7 @@ public int combinationSum4(int[] nums, int target) { **只能进行 k 次的股票交易** -[Leetcode : 188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) +[188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) ```html dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] } @@ -2671,7 +2671,7 @@ public int maxProfit(int k, int[] prices) { **只能进行两次的股票交易** -[Leetcode : 123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) +[123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/) ```java public int maxProfit(int[] prices) { @@ -2691,7 +2691,7 @@ public int maxProfit(int[] prices) { **数组区间和** -[Leetcode : 303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/) +[303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/) ```html Given nums = [-2, 0, 3, -5, 2, -1] @@ -2722,7 +2722,7 @@ class NumArray { **子数组最大的和** -[Leetcode : 53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) +[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) ```html For example, given the array [-2,1,-3,4,-1,2,1,-5,4], @@ -2744,7 +2744,7 @@ public int maxSubArray(int[] nums) { **数组中等差递增子区间的个数** -[Leetcode : 413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/) +[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/) ```html A = [1, 2, 3, 4] @@ -2775,7 +2775,7 @@ public int numberOfArithmeticSlices(int[] A) { **删除两个字符串的字符使它们相等** -[Leetcode : 583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/) +[583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/) ```html Input: "sea", "eat" @@ -2802,7 +2802,7 @@ public int minDistance(String word1, String word2) { **修改一个字符串成为另一个字符串** -[Leetcode : 72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/) +[72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/) ```html Example 1: @@ -2855,7 +2855,7 @@ public int minDistance(String word1, String word2) { **分割整数的最大乘积** -[Leetcode : 343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/) +[343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/) 题目描述:For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). @@ -2874,7 +2874,7 @@ public int integerBreak(int n) { **按平方数来分割整数** -[Leetcode : 279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/) +[279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/) 题目描述:For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. @@ -2908,7 +2908,7 @@ private List generateSquareList(int n) { **分割整数构成字母字符串** -[Leetcode : 91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/) +[91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/) 题目描述:Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). @@ -2934,7 +2934,7 @@ public int numDecodings(String s) { **矩阵的总路径数** -[Leetcode : 62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/) +[62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/) 题目描述:统计从矩阵左上角到右下角的路径总数,每次只能向右或者向下移动。 @@ -2969,7 +2969,7 @@ public int uniquePaths(int m, int n) { **矩阵的最小路径和** -[Leetcode : 64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/) +[64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/) ```html [[1,3,1], @@ -3000,7 +3000,7 @@ public int minPathSum(int[][] grid) { **需要冷却期的股票交易** -[Leetcode : 309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) +[309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) 题目描述:交易之后需要有一天的冷却时间。 @@ -3028,7 +3028,7 @@ public int maxProfit(int[] prices) { **需要交易费用的股票交易** -[Leetcode : 714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) +[714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/) ```html Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 @@ -3066,7 +3066,7 @@ public int maxProfit(int[] prices, int fee) { **买入和售出股票最大的收益** -[Leetcode : 121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) +[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) 只进行一次交易。 @@ -3088,7 +3088,7 @@ public int maxProfit(int[] prices) { **复制粘贴字符** -[Leetcode : 650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/) +[650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/) 题目描述:最开始只有一个字符 A,问需要多少次操作能够得到 n 个字符 A,每次操作可以复制当前所有的字符,或者粘贴。 @@ -3152,7 +3152,7 @@ x 和 y 的最小公倍数为:lcm(x,y) = 2max(m0,n0) \* 3max( **生成素数序列** -[Leetcode : 204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/) +[204. Count Primes (Easy)](https://leetcode.com/problems/count-primes/description/) 埃拉托斯特尼筛法在每次找到一个素数时,将能被素数整除的数排除掉。 @@ -3205,7 +3205,7 @@ int lcm(int a, int b){ **7 进制** -[Leetcode : 504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/) +[504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/) ```java public String convertToBase7(int num) { @@ -3240,7 +3240,7 @@ public String convertToBase7(int num) { **16 进制** -[Leetcode : 405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/) +[405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/) 负数要用它的补码形式。 @@ -3273,7 +3273,7 @@ public String toHex(int num) { **26 进制** -[Leetcode : 168. Excel Sheet Column Title (Easy)](https://leetcode.com/problems/excel-sheet-column-title/description/) +[168. Excel Sheet Column Title (Easy)](https://leetcode.com/problems/excel-sheet-column-title/description/) ```html 1 -> A @@ -3299,7 +3299,7 @@ public String convertToTitle(int n) { **统计阶乘尾部有多少个 0** -[Leetcode : 172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/) +[172. Factorial Trailing Zeroes (Easy)](https://leetcode.com/problems/factorial-trailing-zeroes/description/) 尾部的 0 由 2 * 5 得来,2 的数量明显多于 5 的数量,因此只要统计有多少个 5 即可。 @@ -3317,7 +3317,7 @@ public int trailingZeroes(int n) { **二进制加法** -[Leetcode : 67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/) +[67. Add Binary (Easy)](https://leetcode.com/problems/add-binary/description/) ```html a = "11" @@ -3341,7 +3341,7 @@ public String addBinary(String a, String b) { **字符串加法** -[Leetcode : 415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/) +[415. Add Strings (Easy)](https://leetcode.com/problems/add-strings/description/) 字符串的值为非负整数。 @@ -3363,7 +3363,7 @@ public String addStrings(String num1, String num2) { **改变数组元素使所有的数组元素都相等** -[Leetcode : 462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) +[462. Minimum Moves to Equal Array Elements II (Medium)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/) ```html Input: @@ -3452,7 +3452,7 @@ private void swap(int[] nums, int i, int j) { **数组中出现次数多于 n / 2 的元素** -[Leetcode : 169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/) +[169. Majority Element (Easy)](https://leetcode.com/problems/majority-element/description/) 先对数组排序,最中间那个数出现次数一定多于 n / 2。 @@ -3480,7 +3480,7 @@ public int majorityElement(int[] nums) { **平方数** -[Leetcode : 367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/) +[367. Valid Perfect Square (Easy)](https://leetcode.com/problems/valid-perfect-square/description/) ```html Input: 16 @@ -3506,7 +3506,7 @@ public boolean isPerfectSquare(int num) { **3 的 n 次方** -[Leetcode : 326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/) +[326. Power of Three (Easy)](https://leetcode.com/problems/power-of-three/description/) ```java public boolean isPowerOfThree(int n) { @@ -3516,7 +3516,7 @@ public boolean isPowerOfThree(int n) { **乘积数组** -[Leetcode : 238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/) +[238. Product of Array Except Self (Medium)](https://leetcode.com/problems/product-of-array-except-self/description/) ```html For example, given [1,2,3,4], return [24,12,8,6]. @@ -3547,7 +3547,7 @@ public int[] productExceptSelf(int[] nums) { **找出数组中的乘积最大的三个数** -[Leetcode : 628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/) +[628. Maximum Product of Three Numbers (Easy)](https://leetcode.com/problems/maximum-product-of-three-numbers/description/) ```html Input: [1,2,3,4] @@ -3586,7 +3586,7 @@ public int maximumProduct(int[] nums) { **用栈实现队列** -[Leetcode : 232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/) +[232. Implement Queue using Stacks (Easy)](https://leetcode.com/problems/implement-queue-using-stacks/description/) 一个栈实现: @@ -3657,7 +3657,7 @@ class MyQueue { **用队列实现栈** -[Leetcode : 225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/) +[225. Implement Stack using Queues (Easy)](https://leetcode.com/problems/implement-stack-using-queues/description/) ```java class MyStack { @@ -3692,7 +3692,7 @@ class MyStack { **最小值栈** -[Leetcode : 155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/) +[155. Min Stack (Easy)](https://leetcode.com/problems/min-stack/description/) 用两个栈实现,一个存储数据,一个存储最小值。 @@ -3735,7 +3735,7 @@ class MinStack { **用栈实现括号匹配** -[Leetcode : 20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/) +[20. Valid Parentheses (Easy)](https://leetcode.com/problems/valid-parentheses/description/) ```html "()[]{}" @@ -3768,7 +3768,7 @@ Input: [73, 74, 75, 71, 69, 72, 76, 73] Output: [1, 1, 4, 2, 1, 1, 0, 0] ``` -[Leetcode : 739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/) +[739. Daily Temperatures (Medium)](https://leetcode.com/problems/daily-temperatures/description/) 在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。 @@ -3790,7 +3790,7 @@ public int[] dailyTemperatures(int[] temperatures) { **在另一个数组中比当前元素大的下一个元素** -[Leetcode : 496. Next Greater Element I (Easy)](https://leetcode.com/problems/next-greater-element-i/description/) +[496. Next Greater Element I (Easy)](https://leetcode.com/problems/next-greater-element-i/description/) ```html Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. @@ -3818,7 +3818,7 @@ public int[] nextGreaterElement(int[] nums1, int[] nums2) { **循环数组中比当前元素大的下一个元素** -[Leetcode : 503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/) +[503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/) ```java public int[] nextGreaterElements(int[] nums) { @@ -3851,7 +3851,7 @@ HashMap 也可以用来对元素进行计数统计,此时键为元素,值为 **数组中的两个数和为给定值** -[Leetcode : 1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/) +[1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/description/) 可以先对数组进行排序,然后使用双指针方法或者二分查找方法。这样做的时间复杂度为 O(NlogN),空间复杂度为 O(1)。 @@ -3870,7 +3870,7 @@ public int[] twoSum(int[] nums, int target) { **判断数组是否含有相同元素** -[Leetcode : 217. Contains Duplicate (Easy)](https://leetcode.com/problems/contains-duplicate/description/) +[217. Contains Duplicate (Easy)](https://leetcode.com/problems/contains-duplicate/description/) ```java public boolean containsDuplicate(int[] nums) { @@ -3884,7 +3884,7 @@ public boolean containsDuplicate(int[] nums) { **最长和谐序列** -[Leetcode : 594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/) +[594. Longest Harmonious Subsequence (Easy)](https://leetcode.com/problems/longest-harmonious-subsequence/description/) ```html Input: [1,3,2,2,5,2,3,7] @@ -3912,7 +3912,7 @@ public int findLHS(int[] nums) { **最长连续序列** -[Leetcode : 128. Longest Consecutive Sequence (Hard)](https://leetcode.com/problems/longest-consecutive-sequence/description/) +[128. Longest Consecutive Sequence (Hard)](https://leetcode.com/problems/longest-consecutive-sequence/description/) ```html Given [100, 4, 200, 1, 3, 2], @@ -3955,7 +3955,7 @@ private int count(Map numCnts, int num) { **两个字符串包含的字符是否完全相同** -[Leetcode : 242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/) +[242. Valid Anagram (Easy)](https://leetcode.com/problems/valid-anagram/description/) ```html s = "anagram", t = "nagaram", return true. @@ -3976,7 +3976,7 @@ public boolean isAnagram(String s, String t) { **计算一组字符集合可以组成的回文字符串的最大长度** -[Leetcode : 409. Longest Palindrome (Easy)](https://leetcode.com/problems/longest-palindrome/description/) +[409. Longest Palindrome (Easy)](https://leetcode.com/problems/longest-palindrome/description/) ```html Input : "abccccdd" @@ -3999,7 +3999,7 @@ public int longestPalindrome(String s) { **字符串同构** -[Leetcode : 205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/) +[205. Isomorphic Strings (Easy)](https://leetcode.com/problems/isomorphic-strings/description/) ```html Given "egg", "add", return true. @@ -4025,7 +4025,7 @@ public boolean isIsomorphic(String s, String t) { **判断一个整数是否是回文数** -[Leetcode : 9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/) +[9. Palindrome Number (Easy)](https://leetcode.com/problems/palindrome-number/description/) 题目要求:不能使用额外空间,也就不能将整数转换为字符串进行判断。 @@ -4047,7 +4047,7 @@ public boolean isPalindrome(int x) { **回文子字符串** -[Leetcode : 647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/) +[647. Palindromic Substrings (Medium)](https://leetcode.com/problems/palindromic-substrings/description/) ```html Input: "aaa" @@ -4078,7 +4078,7 @@ private void extendSubstrings(String s, int start, int end) { **统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数** -[Leetcode : 696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/) +[696. Count Binary Substrings (Easy)](https://leetcode.com/problems/count-binary-substrings/description/) ```html Input: "00110011" @@ -4136,7 +4136,7 @@ s1 进行循环移位的结果是 s1s1 的子字符串,因此只要判断 s2 **把数组中的 0 移到末尾** -[Leetcode : 283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/) +[283. Move Zeroes (Easy)](https://leetcode.com/problems/move-zeroes/description/) ```html For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. @@ -4152,7 +4152,7 @@ public void moveZeroes(int[] nums) { **调整矩阵** -[Leetcode : 566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/) +[566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/) ```html Input: @@ -4184,7 +4184,7 @@ public int[][] matrixReshape(int[][] nums, int r, int c) { **找出数组中最长的连续 1** -[Leetcode : 485. Max Consecutive Ones (Easy)](https://leetcode.com/problems/max-consecutive-ones/description/) +[485. Max Consecutive Ones (Easy)](https://leetcode.com/problems/max-consecutive-ones/description/) ```java public int findMaxConsecutiveOnes(int[] nums) { @@ -4199,7 +4199,7 @@ public int findMaxConsecutiveOnes(int[] nums) { **一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出丢失的数和重复的数** -[Leetcode : 645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/) +[645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/) ```html Input: nums = [1,2,2,4] @@ -4236,12 +4236,12 @@ private void swap(int[] nums, int i, int j) { 类似题目: -- [Leetcode :448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/),寻找所有丢失的元素 -- [Leetcode : 442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/),寻找所有重复的元素。 +- [448. Find All Numbers Disappeared in an Array (Easy)](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/),寻找所有丢失的元素 +- [442. Find All Duplicates in an Array (Medium)](https://leetcode.com/problems/find-all-duplicates-in-an-array/description/),寻找所有重复的元素。 **找出数组中重复的数,数组值在 [1, n] 之间** -[Leetcode : 287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/) +[287. Find the Duplicate Number (Medium)](https://leetcode.com/problems/find-the-duplicate-number/description/) 要求不能修改数组,也不能使用额外的空间。 @@ -4283,7 +4283,7 @@ public int findDuplicate(int[] nums) { **有序矩阵查找** -[Leetocde : 240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) +[240. Search a 2D Matrix II (Medium)](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) ```html [ @@ -4309,7 +4309,7 @@ public boolean searchMatrix(int[][] matrix, int target) { **有序矩阵的 Kth Element** -[Leetcode : 378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/) +[378. Kth Smallest Element in a Sorted Matrix ((Medium))](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/) ```html matrix = [ @@ -4375,7 +4375,7 @@ class Tuple implements Comparable { **数组相邻差值的个数** -[Leetcode : 667. Beautiful Arrangement II (Medium)](https://leetcode.com/problems/beautiful-arrangement-ii/description/) +[667. Beautiful Arrangement II (Medium)](https://leetcode.com/problems/beautiful-arrangement-ii/description/) ```html Input: n = 3, k = 2 @@ -4403,7 +4403,7 @@ public int[] constructArray(int n, int k) { **数组的度** -[Leetcode : 697. Degree of an Array (Easy)](https://leetcode.com/problems/degree-of-an-array/description/) +[697. Degree of an Array (Easy)](https://leetcode.com/problems/degree-of-an-array/description/) ```html Input: [1,2,2,3,1,4,2] @@ -4442,7 +4442,7 @@ public int findShortestSubArray(int[] nums) { **对角元素相等的矩阵** -[Leetcode : 766. Toeplitz Matrix (Easy)](https://leetcode.com/problems/toeplitz-matrix/description/) +[766. Toeplitz Matrix (Easy)](https://leetcode.com/problems/toeplitz-matrix/description/) ```html 1234 @@ -4480,7 +4480,7 @@ private boolean check(int[][] matrix, int expectValue, int row, int col) { **嵌套数组** -[Leetcode : 565. Array Nesting (Medium)](https://leetcode.com/problems/array-nesting/description/) +[565. Array Nesting (Medium)](https://leetcode.com/problems/array-nesting/description/) ```html Input: A = [5,4,0,3,1,6,2] @@ -4514,7 +4514,7 @@ public int arrayNesting(int[] nums) { **分隔数组** -[Leetcode : 769. Max Chunks To Make Sorted (Medium)](https://leetcode.com/problems/max-chunks-to-make-sorted/description/) +[769. Max Chunks To Make Sorted (Medium)](https://leetcode.com/problems/max-chunks-to-make-sorted/description/) ```html Input: arr = [1,0,2,3,4] @@ -4545,7 +4545,7 @@ public int maxChunksToSorted(int[] arr) { **找出两个链表的交点** -[Leetcode : 160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) +[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/) ```html A: a1 → a2 @@ -4576,7 +4576,7 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) { **链表反转** -[Leetcode : 206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/) +[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/) 递归 @@ -4608,7 +4608,7 @@ public ListNode reverseList(ListNode head) { **归并两个有序的链表** -[Leetcode : 21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/) +[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/) ```java public ListNode mergeTwoLists(ListNode l1, ListNode l2) { @@ -4626,7 +4626,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { **从有序链表中删除重复节点** -[Leetcode : 83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) +[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) ```html Given 1->1->2, return 1->2. @@ -4643,7 +4643,7 @@ public ListNode deleteDuplicates(ListNode head) { **删除链表的倒数第 n 个节点** -[Leetcode : 19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) +[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) ```html Given linked list: 1->2->3->4->5, and n = 2. @@ -4670,7 +4670,7 @@ public ListNode removeNthFromEnd(ListNode head, int n) { **交换链表中的相邻结点** -[Leetcode : 24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/) +[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/) ```html Given 1->2->3->4, you should return the list as 2->1->4->3. @@ -4697,7 +4697,7 @@ public ListNode swapPairs(ListNode head) { **链表求和** -[Leetcode : 445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/) +[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/) ```html Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) @@ -4736,7 +4736,7 @@ private Stack buildStack(ListNode l) { **回文链表** -[Leetcode : 234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/) +[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/) 要求以 O(1) 的空间复杂度来求解。 @@ -4789,7 +4789,7 @@ private boolean isEqual(ListNode l1, ListNode l2) { **链表元素按奇偶聚集** -[Leetcode : 328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/) +[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/) ```html Example: @@ -4816,7 +4816,7 @@ public ListNode oddEvenList(ListNode head) { **分隔链表** -[Leetcode : 725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/) +[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/) ```html Input: @@ -4862,7 +4862,7 @@ public ListNode[] splitListToParts(ListNode root, int k) { **树的高度** -[Leetcode : 104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) +[104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) ```java public int maxDepth(TreeNode root) { @@ -4873,7 +4873,7 @@ public int maxDepth(TreeNode root) { **翻转树** -[Leetcode : 226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/) +[226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/) ```java public TreeNode invertTree(TreeNode root) { @@ -4887,7 +4887,7 @@ public TreeNode invertTree(TreeNode root) { **归并两棵树** -[Leetcode : 617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/) +[617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/) ```html Input: @@ -4946,7 +4946,7 @@ public boolean hasPathSum(TreeNode root, int sum) { **统计路径和等于一个数的路径数量** -[Leetcode : 437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/) +[437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/) ```html root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 @@ -4986,7 +4986,7 @@ private int pathSumStartWithRoot(TreeNode root, int sum){ **子树** -[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/) +[572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/) ```html Given tree s: @@ -5033,7 +5033,7 @@ private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) { **树的对称** -[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/) +[101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/) ```html 1 @@ -5059,7 +5059,7 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2){ **平衡树** -[Leetcode : 110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/) +[110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/) ```html 3 @@ -5090,7 +5090,7 @@ public int maxDepth(TreeNode root) { **最小路径** -[Leetcode : 111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) +[111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) 树的根节点到叶子节点的最小路径长度 @@ -5106,7 +5106,7 @@ public int minDepth(TreeNode root) { **统计左叶子节点的和** -[Leetcode : 404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/) +[404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/) ```html 3 @@ -5133,7 +5133,7 @@ private boolean isLeaf(TreeNode node){ **修剪二叉查找树** -[Leetcode : 669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/) +[669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/) ```html Input: @@ -5173,7 +5173,7 @@ public TreeNode trimBST(TreeNode root, int L, int R) { **从有序数组中构造二叉查找树** -[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) +[108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) ```java public TreeNode sortedArrayToBST(int[] nums) { @@ -5192,7 +5192,7 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){ **两节点的最长路径** -[Leetcode : 543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/) +[543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/) ```html Input: @@ -5224,7 +5224,7 @@ private int depth(TreeNode root) { **找出二叉树中第二小的节点** -[Leetcode : 671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) +[671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) ```html Input: @@ -5255,7 +5255,7 @@ public int findSecondMinimumValue(TreeNode root) { **二叉查找树的最近公共祖先** -[Leetcode : 235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) +[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) ```html _______6______ @@ -5278,7 +5278,7 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { **二叉树的最近公共祖先** -[Leetcode : 236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) +[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) ```html _______3______ @@ -5302,7 +5302,7 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { **相同节点值的最大路径长度** -[Leetcode : 687. Longest Univalue Path (Easy)](https://leetcode.com/problems/longest-univalue-path/) +[687. Longest Univalue Path (Easy)](https://leetcode.com/problems/longest-univalue-path/) ```html 1 @@ -5335,7 +5335,7 @@ private int dfs(TreeNode root){ **间隔遍历** -[Leetcode : 337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/) +[337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/) ```html 3 @@ -5392,7 +5392,7 @@ public List averageOfLevels(TreeNode root) { **得到左下角的节点** -[Leetcode : 513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/) +[513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/) ```html Input: @@ -5473,7 +5473,7 @@ void dfs(TreeNode root){ **非递归实现二叉树的前序遍历** -[Leetcode : 144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) +[144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) ```java public List preorderTraversal(TreeNode root) { @@ -5493,7 +5493,7 @@ public List preorderTraversal(TreeNode root) { **非递归实现二叉树的后序遍历** -[Leetcode : 145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) +[145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) 前序遍历为 root -> left -> right,后序遍历为 left -> right -> root,可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。 @@ -5516,7 +5516,7 @@ public List postorderTraversal(TreeNode root) { **非递归实现二叉树的中序遍历** -[Leetcode : 94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) +[94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) ```java public List inorderTraversal(TreeNode root) { @@ -5586,7 +5586,7 @@ private void inOrder(TreeNode root, List nums){ **在 BST 中查找两个节点之差的最小绝对值** -[Leetcode : 530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) +[530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) ```html Input: @@ -5622,7 +5622,7 @@ private void inorder(TreeNode node){ **把 BST 每个节点的值都加上比它大的节点的值** -[Leetcode : Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) +[Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) ```html Input: The root of a Binary Search Tree like this: @@ -5657,7 +5657,7 @@ private void traver(TreeNode root) { **寻找 BST 中出现次数最多的节点** -[Leetcode : 501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) +[501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) ```html 1 @@ -5706,7 +5706,7 @@ private void inOrder(TreeNode node) { **寻找 BST 的第 k 个元素** -[Leetcode : 230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) +[230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) 递归解法: @@ -5749,7 +5749,7 @@ private void inOrder(TreeNode node, int k) { **根据有序链表构造平衡的 BST** -[Leetcode : 109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) +[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) ```html Given the sorted linked list: [-10,-3,0,5,9], @@ -5801,7 +5801,7 @@ Trie,又称前缀树或字典树,用于判断字符串是否存在或者是 **实现一个 Trie** -[Leetcode : 208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/) +[208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/) ```java class Trie { @@ -5863,7 +5863,7 @@ class Trie { **实现一个 Trie,用来求前缀和** -[Leetcode : 677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/) +[677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/) ```html Input: insert("apple", 3), Output: Null @@ -5934,7 +5934,7 @@ class MapSum { **判断是否为二分图** -[Leetcode : 785. Is Graph Bipartite? (Medium)](https://leetcode.com/problems/is-graph-bipartite/description/) +[785. Is Graph Bipartite? (Medium)](https://leetcode.com/problems/is-graph-bipartite/description/) ```html Input: [[1,3], [0,2], [1,3], [0,2]] @@ -5992,7 +5992,7 @@ private boolean isBipartite(int[][] graph, int cur, int color, int[] colors) { **课程安排的合法性** -[Leetcode : 207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule/description/) +[207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule/description/) ```html 2, [[1,0]] @@ -6046,7 +6046,7 @@ private boolean dfs(boolean[] globalMarked, boolean[] localMarked, List **课程安排的顺序** -[Leetcode : 210. Course Schedule II (Medium)](https://leetcode.com/problems/course-schedule-ii/description/) +[210. Course Schedule II (Medium)](https://leetcode.com/problems/course-schedule-ii/description/) ```html 4, [[1,0],[2,0],[3,1],[3,2]] @@ -6104,7 +6104,7 @@ private boolean dfs(boolean[] globalMarked, boolean[] localMarked, List **冗余连接** -[Leetcode : 684. Redundant Connection (Medium)](https://leetcode.com/problems/redundant-connection/description/) +[684. Redundant Connection (Medium)](https://leetcode.com/problems/redundant-connection/description/) ```html Input: [[1,2], [1,3], [2,3]] @@ -6210,7 +6210,7 @@ static String toBinaryString(int i); // 转换为二进制表示的字符串 **统计两个数的二进制表示有多少位不同** -[Leetcode : 461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/) +[461. Hamming Distance (Easy)](https://leetcode.com/problems/hamming-distance/) ```html Input: x = 1, y = 4 @@ -6263,7 +6263,7 @@ public int hammingDistance(int x, int y) { **数组中唯一一个不重复的元素** -[Leetcode : 136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/) +[136. Single Number (Easy)](https://leetcode.com/problems/single-number/description/) ```html Input: [4,1,2,1,2] @@ -6282,7 +6282,7 @@ public int singleNumber(int[] nums) { **找出数组中缺失的那个数** -[Leetcode : 268. Missing Number (Easy)](https://leetcode.com/problems/missing-number/description/) +[268. Missing Number (Easy)](https://leetcode.com/problems/missing-number/description/) ```html Input: [3,0,1] @@ -6303,7 +6303,7 @@ public int missingNumber(int[] nums) { **数组中不重复的两个元素** -[Leetcode : 260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/) +[260. Single Number III (Medium)](https://leetcode.com/problems/single-number-iii/description/) 两个不相等的元素在位级表示上必定会有一位存在不同。 @@ -6328,7 +6328,7 @@ public int[] singleNumber(int[] nums) { **翻转一个数的比特位** -[Leetcode : 190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/) +[190. Reverse Bits (Easy)](https://leetcode.com/problems/reverse-bits/description/) ```java public int reverseBits(int n) { @@ -6385,7 +6385,7 @@ a = a ^ b; **判断一个数是不是 2 的 n 次方** -[Leetcode : 231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/) +[231. Power of Two (Easy)](https://leetcode.com/problems/power-of-two/description/) 二进制表示只有一个 1 存在。 @@ -6405,7 +6405,7 @@ public boolean isPowerOfTwo(int n) { **判断一个数是不是 4 的 n 次方** -[Leetcode : 342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/) +[342. Power of Four (Easy)](https://leetcode.com/problems/power-of-four/) 这种数在二进制表示中有且只有一个奇数位为 1,例如 16(10000)。 @@ -6426,7 +6426,7 @@ public boolean isPowerOfFour(int num) { **判断一个数的位级表示是否不会出现连续的 0 和 1** -[Leetcode : 693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) +[693. Binary Number with Alternating Bits (Easy)](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) ```html Input: 10 @@ -6451,7 +6451,7 @@ public boolean hasAlternatingBits(int n) { **求一个数的补码** -[Leetcode : 476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/) +[476. Number Complement (Easy)](https://leetcode.com/problems/number-complement/description/) ```html Input: 5 @@ -6506,7 +6506,7 @@ public int findComplement(int num) { **实现整数的加法** -[Leetcode : 371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/) +[371. Sum of Two Integers (Easy)](https://leetcode.com/problems/sum-of-two-integers/description/) a ^ b 表示没有考虑进位的情况下两数的和,(a & b) << 1 就是进位。 @@ -6520,7 +6520,7 @@ public int getSum(int a, int b) { **字符串数组最大乘积** -[Leetcode : 318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) +[318. Maximum Product of Word Lengths (Medium)](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) ```html Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"] @@ -6555,7 +6555,7 @@ public int maxProduct(String[] words) { **统计从 0 \~ n 每个数的二进制表示中 1 的个数** -[Leetcode : 338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/) +[338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/description/) 对于数字 6(110),它可以看成是 4(100) 再加一个 2(10),因此 dp[i] = dp[i&(i-1)] + 1; -- GitLab