diff --git a/README.md b/README.md index 58f71049..4fa9100d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,11 @@

+

+ + + + # LeetCode 刷题攻略 diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 3cd687a6..e15f692e 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -95,7 +95,32 @@ public: Java: +```java + /** + * 1.dp[i]代表当前下标对应的最大值 + * 2.递推公式 dp[i] = max (dp[i-1]+nums[i],nums[i]) res = max(res,dp[i]) + * 3.初始化 都为 0 + * 4.遍历方向,从前往后 + * 5.举例推导结果。。。 + * + * @param nums + * @return + */ + public static int maxSubArray(int[] nums) { + if (nums.length == 0) { + return 0; + } + int res = nums[0]; + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); + res = res > dp[i] ? res : dp[i]; + } + return res; + } +``` Python: diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index c7fdf776..89d0dda7 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -98,15 +98,13 @@ class Solution: out_list = [] while quene: + length = len(queue) # 这里一定要先求出队列的长度,不能用range(len(queue)),因为queue长度是变化的 in_list = [] - for _ in range(len(quene)): - node = quene.pop(0) - in_list.append(node.val) - if node.left: - quene.append(node.left) - if node.right: - quene.append(node.right) - + for _ in range(length): + curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个 + in_list.append(curnode.val) + if curnode.left: queue.append(curnode.left) + if curnode.right: queue.append(curnode.right) out_list.append(in_list) return out_list diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 812c58ca..463b55d9 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -193,40 +193,6 @@ public: }; ``` -使用栈来模拟后序遍历依然可以 - -```C++ -class Solution { -public: - int maxDepth(TreeNode* root) { - stack st; - if (root != NULL) st.push(root); - int depth = 0; - int result = 0; - while (!st.empty()) { - TreeNode* node = st.top(); - if (node != NULL) { - st.pop(); - st.push(node); // 中 - st.push(NULL); - depth++; - if (node->right) st.push(node->right); // 右 - if (node->left) st.push(node->left); // 左 - - } else { - st.pop(); - node = st.top(); - st.pop(); - depth--; - } - result = result > depth ? result : depth; - } - return result; - - } -}; -``` - ## 其他语言版本 diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index bbca5ea0..4d283eb0 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -138,20 +138,16 @@ class Solution { ``` Python: -```python +```python3 class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: - #贪心 求波峰数量 + 波谷数量 - if len(nums)<=1: - return len(nums) - cur, pre = 0,0 #当前一对差值,前一对差值 - count = 1#默认最右边有一个峰值 - for i in range(len(nums)-1): - cur = nums[i+1] - nums[i] - if((cur>0 and pre<=0) or (cur<0 and pre>=0)): - count += 1 - pre = cur - return count + preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度 + for i in range(len(nums) - 1): + curC = nums[i + 1] - nums[i] + if curC * preC <= 0 and curC !=0: #差值为0时,不算摆动 + res += 1 + preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值 + return res ``` Go: diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 23e2c5fd..2f3c4f4d 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -22,13 +22,13 @@ https://leetcode-cn.com/problems/ransom-note/ 你可以假设两个字符串均只含有小写字母。 -canConstruct("a", "b") -> false -canConstruct("aa", "ab") -> false -canConstruct("aa", "aab") -> true +canConstruct("a", "b") -> false +canConstruct("aa", "ab") -> false +canConstruct("aa", "aab") -> true ## 思路 -这道题目和[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)很像,[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)相当于求 字符串a 和 字符串b 是否可以相互组成 ,而这道题目是求 字符串a能否组成字符串b,而不用管字符串b 能不能组成字符串a。 +这道题目和[242.有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA)很像,[242.有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA)相当于求 字符串a 和 字符串b 是否可以相互组成 ,而这道题目是求 字符串a能否组成字符串b,而不用管字符串b 能不能组成字符串a。 本题判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成,但是这里需要注意两点。 @@ -75,7 +75,7 @@ public: 依然是数组在哈希法中的应用。 -一些同学可能想,用数组干啥,都用map完事了,**其实在本题的情况下,使用map的空间消耗要比数组大一些的,因为map要维护红黑树或者哈希表,而且还要做哈希函数。 所以数组更加简单直接有效!** +一些同学可能想,用数组干啥,都用map完事了,**其实在本题的情况下,使用map的空间消耗要比数组大一些的,因为map要维护红黑树或者哈希表,而且还要做哈希函数,是费时的!数据量大的话就能体现出来差别了。 所以数组更加简单直接有效!** 代码如下: diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index ecf7f132..4814d414 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -134,22 +134,17 @@ class Solution { ``` Python: -```python +```python3 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: - #先考虑胃口小的孩子 g.sort() s.sort() - i=j=0 - count = 0 - while(i= g[res]: #小饼干先喂饱小胃口 + res += 1 + return res ``` - Go: Javascript: diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 4ab4a6df..08043ea3 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -33,7 +33,7 @@ 示例 3: 输入: amount = 10, coins = [10] 输出: 1 -  + 注意,你可以假设: * 0 <= amount (总金额) <= 5000 @@ -101,7 +101,7 @@ dp[j] (考虑coins[i]的组合总和) 就是所有的dp[j - coins[i]](不 而本题要求凑成总和的组合数,元素之间要求没有顺序。 -所以纯完全背包是能凑成总结就行,不用管怎么凑的。 +所以纯完全背包是能凑成总和就行,不用管怎么凑的。 本题是求凑出来的方案个数,且每个方案个数是为组合数。 @@ -208,6 +208,21 @@ class Solution { Python: +```python3 +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + dp = [0]*(amount + 1) + dp[0] = 1 + # 遍历物品 + for i in range(len(coins)): + # 遍历背包 + for j in range(coins[i], amount + 1): + dp[j] += dp[j - coins[i]] + return dp[amount] +``` + + + Go: ```go func change(amount int, coins []int) int { diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 0442ceaa..931e6e5c 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -13,16 +13,21 @@ 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 - 示例 1: + +``` 输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nums 中并且下标为 4 +``` 示例 2: + +``` 输入: nums = [-1,0,3,5,9,12], target = 2 输出: -1 解释: 2 不存在 nums 中因此返回 -1 +``` 提示: @@ -146,7 +151,7 @@ public: ## 其他语言版本 -Java: +**Java:** (版本一)左闭右闭区间 @@ -192,9 +197,11 @@ class Solution { } ``` -Python: +**Python:** -```python3 +(版本一)左闭右闭区间 + +```python class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 @@ -211,8 +218,26 @@ class Solution: return -1 ``` +(版本二)左闭右开区间 -Go: +```python +class Solution: + def search(self, nums: List[int], target: int) -> int: + left,right =0, len(nums) + while left < right: + mid = (left + right) // 2 + if nums[mid] < target: + left = mid+1 + elif nums[mid] > target: + right = mid + else: + return mid + return -1 +``` + + + +**Go:** (版本一)左闭右闭区间 @@ -254,7 +279,7 @@ func search(nums []int, target int) int { } ``` -javaScript +**javaScript:** ```js diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 1e8e8038..4238a389 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -255,7 +255,18 @@ func min(a, b int) int { } ``` - +Javascript: +```Javascript +var minCostClimbingStairs = function(cost) { + const dp = [ cost[0], cost[1] ] + + for (let i = 2; i < cost.length; ++i) { + dp[i] = Math.min(dp[i -1] + cost[i], dp[i - 2] + cost[i]) + } + + return Math.min(dp[cost.length - 1], dp[cost.length - 2]) +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index faee2e56..a18c008d 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -184,7 +184,38 @@ class Solution: Go: +Javascript: +```Javascript +var lemonadeChange = function(bills) { + let fiveCount = 0 + let tenCount = 0 + for(let i = 0; i < bills.length; i++) { + let bill = bills[i] + if(bill === 5) { + fiveCount += 1 + } else if (bill === 10) { + if(fiveCount > 0) { + fiveCount -=1 + tenCount += 1 + } else { + return false + } + } else { + if(tenCount > 0 && fiveCount > 0) { + tenCount -= 1 + fiveCount -= 1 + } else if(fiveCount >= 3) { + fiveCount -= 3 + } else { + return false + } + } + } + return true +}; + +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 78f34e71..c6779427 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -32,11 +32,11 @@ 看如下两个链表,目前curA指向链表A的头结点,curB指向链表B的头结点: -![面试题02.07.链表相交_1](https://code-thinking.cdn.bcebos.com/pics/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4_1.png)v +![面试题02.07.链表相交_1](https://code-thinking.cdn.bcebos.com/pics/面试题02.07.链表相交_1.png) 我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,如图: -![面试题02.07.链表相交_2](https://code-thinking.cdn.bcebos.com/pics/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4_2.png) +![面试题02.07.链表相交_2](https://code-thinking.cdn.bcebos.com/pics/面试题02.07.链表相交_2.png) 此时我们就可以比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到焦点。