Merge branch 'youngyangyang04:master' into master

This commit is contained in:
zhangzw001
2021-06-08 17:09:05 +08:00
12 changed files with 148 additions and 81 deletions

View File

@ -17,6 +17,11 @@
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a> <a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a> <a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
</p> </p>
<p align="center">
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ" target="_blank">
<img src="./pics/知识星球.png" width="600"/>
</a>
# LeetCode 刷题攻略 # LeetCode 刷题攻略

View File

@ -95,7 +95,32 @@ public:
Java 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 Python

View File

@ -98,15 +98,13 @@ class Solution:
out_list = [] out_list = []
while quene: while quene:
length = len(queue) # 这里一定要先求出队列的长度不能用range(len(queue))因为queue长度是变化的
in_list = [] in_list = []
for _ in range(len(quene)): for _ in range(length):
node = quene.pop(0) curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个
in_list.append(node.val) in_list.append(curnode.val)
if node.left: if curnode.left: queue.append(curnode.left)
quene.append(node.left) if curnode.right: queue.append(curnode.right)
if node.right:
quene.append(node.right)
out_list.append(in_list) out_list.append(in_list)
return out_list return out_list

View File

@ -193,40 +193,6 @@ public:
}; };
``` ```
使用栈来模拟后序遍历依然可以
```C++
class Solution {
public:
int maxDepth(TreeNode* root) {
stack<TreeNode*> 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;
}
};
```
## 其他语言版本 ## 其他语言版本

View File

@ -138,20 +138,16 @@ class Solution {
``` ```
Python Python
```python ```python3
class Solution: class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int: def wiggleMaxLength(self, nums: List[int]) -> int:
#贪心 求波峰数量 + 波谷数量 preC,curC,res = 0,0,1 #题目里nums长度大于等于1当长度为1时其实到不了for循环里去所以不用考虑nums长度
if len(nums)<=1:
return len(nums)
cur, pre = 0,0 #当前一对差值,前一对差值
count = 1#默认最右边有一个峰值
for i in range(len(nums) - 1): for i in range(len(nums) - 1):
cur = nums[i+1] - nums[i] curC = nums[i + 1] - nums[i]
if((cur>0 and pre<=0) or (cur<0 and pre>=0)): if curC * preC <= 0 and curC !=0: #差值为0时不算摆动
count += 1 res += 1
pre = cur preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值
return count return res
``` ```
Go Go

View File

@ -28,7 +28,7 @@ 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里面的字符构成但是这里需要注意两点。 本题判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成但是这里需要注意两点。
@ -75,7 +75,7 @@ public:
依然是数组在哈希法中的应用。 依然是数组在哈希法中的应用。
一些同学可能想用数组干啥都用map完事了**其实在本题的情况下使用map的空间消耗要比数组大一些的因为map要维护红黑树或者哈希表而且还要做哈希函数。 所以数组更加简单直接有效!** 一些同学可能想用数组干啥都用map完事了**其实在本题的情况下使用map的空间消耗要比数组大一些的因为map要维护红黑树或者哈希表而且还要做哈希函数,是费时的!数据量大的话就能体现出来差别了。 所以数组更加简单直接有效!**
代码如下: 代码如下:

View File

@ -134,22 +134,17 @@ class Solution {
``` ```
Python Python
```python ```python3
class Solution: class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int: def findContentChildren(self, g: List[int], s: List[int]) -> int:
#先考虑胃口小的孩子
g.sort() g.sort()
s.sort() s.sort()
i=j=0 res = 0
count = 0 for i in range(len(s)):
while(i<len(g) and j<len(s)): if res <len(g) and s[i] >= g[res]: #小饼干先喂饱小胃口
if g[i]<=s[j]: res += 1
count+=1 return res
i+=1 #如果满足了,则继续遍历下一个孩子和下一块饼干
j += 1 #如果最小的饼干没有满足当前最小胃口的孩子,则遍历下一块饼干
return count
``` ```
Go Go
Javascript: Javascript:

View File

@ -33,7 +33,7 @@
示例 3: 示例 3:
输入: amount = 10, coins = [10] 输入: amount = 10, coins = [10]
输出: 1 输出: 1
 
注意,你可以假设: 注意,你可以假设:
* 0 <= amount (总金额) <= 5000 * 0 <= amount (总金额) <= 5000
@ -101,7 +101,7 @@ dp[j] 考虑coins[i]的组合总和) 就是所有的dp[j - coins[i]](不
而本题要求凑成总和的组合数,元素之间要求没有顺序。 而本题要求凑成总和的组合数,元素之间要求没有顺序。
所以纯完全背包是能凑成总就行,不用管怎么凑的。 所以纯完全背包是能凑成总就行,不用管怎么凑的。
本题是求凑出来的方案个数,且每个方案个数是为组合数。 本题是求凑出来的方案个数,且每个方案个数是为组合数。
@ -208,6 +208,21 @@ class Solution {
Python 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
```go ```go
func change(amount int, coins []int) int { func change(amount int, coins []int) int {

View File

@ -13,16 +13,21 @@
给定一个 n 个元素有序的升序整型数组 nums 和一个目标值 target  写一个函数搜索 nums 中的 target如果目标值存在返回下标否则返回 -1。 给定一个 n 个元素有序的升序整型数组 nums 和一个目标值 target  写一个函数搜索 nums 中的 target如果目标值存在返回下标否则返回 -1。
示例 1: 示例 1:
```
输入: nums = [-1,0,3,5,9,12], target = 9 输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4 输出: 4
解释: 9 出现在 nums 中并且下标为 4 解释: 9 出现在 nums 中并且下标为 4
```
示例 2: 示例 2:
```
输入: nums = [-1,0,3,5,9,12], target = 2 输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1 输出: -1
解释: 2 不存在 nums 中因此返回 -1 解释: 2 不存在 nums 中因此返回 -1
```
提示: 提示:
@ -146,7 +151,7 @@ public:
## 其他语言版本 ## 其他语言版本
Java **Java**
(版本一)左闭右闭区间 (版本一)左闭右闭区间
@ -192,9 +197,11 @@ class Solution {
} }
``` ```
Python **Python**
```python3 (版本一)左闭右闭区间
```python
class Solution: class Solution:
def search(self, nums: List[int], target: int) -> int: def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1 left, right = 0, len(nums) - 1
@ -211,8 +218,26 @@ class Solution:
return -1 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 ```js

View File

@ -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) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -184,7 +184,38 @@ class Solution:
Go 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) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -32,11 +32,11 @@
看如下两个链表目前curA指向链表A的头结点curB指向链表B的头结点 看如下两个链表目前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 末尾对齐的位置,如图: 我们求出两个链表的长度并求出两个链表长度的差值然后让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则找到焦点。 此时我们就可以比较curA和curB是否相同如果不相同同时向后移动curA和curB如果遇到curA == curB则找到焦点。