mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 21:50:49 +08:00
Merge branch 'master' into patch-29
This commit is contained in:
@ -146,19 +146,24 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
|
||||||
|
|
||||||
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
def climbStairs(self, n: int) -> int:
|
def climbStairs(self, n: int) -> int:
|
||||||
m = 2
|
dp = [0]*(n + 1)
|
||||||
dp = [0] * (n + 1)
|
|
||||||
dp[0] = 1
|
dp[0] = 1
|
||||||
for i in range(n + 1):
|
m = 2
|
||||||
for j in range(1, m + 1):
|
# 遍历背包
|
||||||
if i >= j:
|
for j in range(n + 1):
|
||||||
dp[i] += dp[i - j]
|
# 遍历物品
|
||||||
return dp[-1]
|
for step in range(1, m + 1):
|
||||||
|
if j >= step:
|
||||||
|
dp[j] += dp[j - step]
|
||||||
|
return dp[n]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func climbStairs(n int) int {
|
func climbStairs(n int) int {
|
||||||
|
@ -228,7 +228,22 @@ public int minDistance(String word1, String word2) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def minDistance(self, word1: str, word2: str) -> int:
|
||||||
|
dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)]
|
||||||
|
for i in range(len(word1)+1):
|
||||||
|
dp[i][0] = i
|
||||||
|
for j in range(len(word2)+1):
|
||||||
|
dp[0][j] = j
|
||||||
|
for i in range(1, len(word1)+1):
|
||||||
|
for j in range(1, len(word2)+1):
|
||||||
|
if word1[i-1] == word2[j-1]:
|
||||||
|
dp[i][j] = dp[i-1][j-1]
|
||||||
|
else:
|
||||||
|
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
|
||||||
|
return dp[-1][-1]
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
@ -183,7 +183,7 @@ public:
|
|||||||
int end = 0; // 反转的单词在字符串里终止位置
|
int end = 0; // 反转的单词在字符串里终止位置
|
||||||
bool entry = false; // 标记枚举字符串的过程中是否已经进入了单词区间
|
bool entry = false; // 标记枚举字符串的过程中是否已经进入了单词区间
|
||||||
for (int i = 0; i < s.size(); i++) { // 开始反转单词
|
for (int i = 0; i < s.size(); i++) { // 开始反转单词
|
||||||
if ((!entry))) {
|
if (!entry) {
|
||||||
start = i; // 确定单词起始位置
|
start = i; // 确定单词起始位置
|
||||||
entry = true; // 进入单词区间
|
entry = true; // 进入单词区间
|
||||||
}
|
}
|
||||||
@ -380,4 +380,4 @@ func reverse(b *[]byte, left, right int) {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -118,7 +118,7 @@ class Solution {
|
|||||||
if (nums == null || nums.length == 0) return 0;
|
if (nums == null || nums.length == 0) return 0;
|
||||||
if (nums.length == 1) return nums[0];
|
if (nums.length == 1) return nums[0];
|
||||||
|
|
||||||
int[] dp = new int[nums.length + 1];
|
int[] dp = new int[nums.length];
|
||||||
dp[0] = nums[0];
|
dp[0] = nums[0];
|
||||||
dp[1] = Math.max(dp[0], nums[1]);
|
dp[1] = Math.max(dp[0], nums[1]);
|
||||||
for (int i = 2; i < nums.length; i++) {
|
for (int i = 2; i < nums.length; i++) {
|
||||||
|
@ -226,11 +226,11 @@ func coinChange1(coins []int, amount int) int {
|
|||||||
for i := 0; i < len(coins); i++ {
|
for i := 0; i < len(coins); i++ {
|
||||||
// 遍历背包
|
// 遍历背包
|
||||||
for j := coins[i]; j <= amount; j++ {
|
for j := coins[i]; j <= amount; j++ {
|
||||||
//if dp[j-coins[i]] != math.MaxInt32 {
|
if dp[j-coins[i]] != math.MaxInt32 {
|
||||||
// 推导公式
|
// 推导公式
|
||||||
dp[j] = min(dp[j], dp[j-coins[i]]+1)
|
dp[j] = min(dp[j], dp[j-coins[i]]+1)
|
||||||
fmt.Println(dp,j,i)
|
//fmt.Println(dp,j,i)
|
||||||
//}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 没找到能装满背包的, 就返回-1
|
// 没找到能装满背包的, 就返回-1
|
||||||
|
@ -144,7 +144,20 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isSubsequence(self, s: str, t: str) -> bool:
|
||||||
|
dp = [[0] * (len(t)+1) for _ in range(len(s)+1)]
|
||||||
|
for i in range(1, len(s)+1):
|
||||||
|
for j in range(1, len(t)+1):
|
||||||
|
if s[i-1] == t[j-1]:
|
||||||
|
dp[i][j] = dp[i-1][j-1] + 1
|
||||||
|
else:
|
||||||
|
dp[i][j] = dp[i][j-1]
|
||||||
|
if dp[-1][-1] == len(s):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -170,7 +170,20 @@ public class Solution {
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def longestPalindromeSubseq(self, s: str) -> int:
|
||||||
|
dp = [[0] * len(s) for _ in range(len(s))]
|
||||||
|
for i in range(len(s)):
|
||||||
|
dp[i][i] = 1
|
||||||
|
for i in range(len(s)-1, -1, -1):
|
||||||
|
for j in range(i+1, len(s)):
|
||||||
|
if s[i] == s[j]:
|
||||||
|
dp[i][j] = dp[i+1][j-1] + 2
|
||||||
|
else:
|
||||||
|
dp[i][j] = max(dp[i+1][j], dp[i][j-1])
|
||||||
|
return dp[0][-1]
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
@ -107,7 +107,22 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def minDistance(self, word1: str, word2: str) -> int:
|
||||||
|
dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)]
|
||||||
|
for i in range(len(word1)+1):
|
||||||
|
dp[i][0] = i
|
||||||
|
for j in range(len(word2)+1):
|
||||||
|
dp[0][j] = j
|
||||||
|
for i in range(1, len(word1)+1):
|
||||||
|
for j in range(1, len(word2)+1):
|
||||||
|
if word1[i-1] == word2[j-1]:
|
||||||
|
dp[i][j] = dp[i-1][j-1]
|
||||||
|
else:
|
||||||
|
dp[i][j] = min(dp[i-1][j-1] + 2, dp[i-1][j] + 1, dp[i][j-1] + 1)
|
||||||
|
return dp[-1][-1]
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -284,6 +284,56 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
> 动态规划:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def countSubstrings(self, s: str) -> int:
|
||||||
|
dp = [[False] * len(s) for _ in range(len(s))]
|
||||||
|
result = 0
|
||||||
|
for i in range(len(s)-1, -1, -1): #注意遍历顺序
|
||||||
|
for j in range(i, len(s)):
|
||||||
|
if s[i] == s[j]:
|
||||||
|
if j - i <= 1: #情况一 和 情况二
|
||||||
|
result += 1
|
||||||
|
dp[i][j] = True
|
||||||
|
elif dp[i+1][j-1]: #情况三
|
||||||
|
result += 1
|
||||||
|
dp[i][j] = True
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
> 动态规划:简洁版
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def countSubstrings(self, s: str) -> int:
|
||||||
|
dp = [[False] * len(s) for _ in range(len(s))]
|
||||||
|
result = 0
|
||||||
|
for i in range(len(s)-1, -1, -1): #注意遍历顺序
|
||||||
|
for j in range(i, len(s)):
|
||||||
|
if s[i] == s[j] and (j - i <= 1 or dp[i+1][j-1]):
|
||||||
|
result += 1
|
||||||
|
dp[i][j] = True
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
> 双指针法:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def countSubstrings(self, s: str) -> int:
|
||||||
|
result = 0
|
||||||
|
for i in range(len(s)):
|
||||||
|
result += self.extend(s, i, i, len(s)) #以i为中心
|
||||||
|
result += self.extend(s, i, i+1, len(s)) #以i和i+1为中心
|
||||||
|
return result
|
||||||
|
|
||||||
|
def extend(self, s, i, j, n):
|
||||||
|
res = 0
|
||||||
|
while i >= 0 and j < n and s[i] == s[j]:
|
||||||
|
i -= 1
|
||||||
|
j += 1
|
||||||
|
res += 1
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
@ -180,7 +180,36 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
/**
|
||||||
|
* 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑
|
||||||
|
* <p>
|
||||||
|
* 入站元素要和当前栈内栈首元素进行比较
|
||||||
|
* 若大于栈首则 则与元素下标做差
|
||||||
|
* 若大于等于则放入
|
||||||
|
*
|
||||||
|
* @param temperatures
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[] dailyTemperatures(int[] temperatures) {
|
||||||
|
Stack<Integer> stack = new Stack<>();
|
||||||
|
int[] res = new int[temperatures.length];
|
||||||
|
for (int i = 0; i < temperatures.length; i++) {
|
||||||
|
/**
|
||||||
|
* 取出下标进行元素值的比较
|
||||||
|
*/
|
||||||
|
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
|
||||||
|
int preIndex = stack.pop();
|
||||||
|
res[preIndex] = i - preIndex;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 注意 放入的是元素位置
|
||||||
|
*/
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
@ -96,8 +96,46 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public int[] sortedSquares(int[] nums) {
|
||||||
|
int right = nums.length - 1;
|
||||||
|
int left = 0;
|
||||||
|
int[] result = new int[nums.length];
|
||||||
|
int index = result.length - 1;
|
||||||
|
while (left <= right) {
|
||||||
|
if (nums[left] * nums[left] > nums[right] * nums[right]) {
|
||||||
|
result[index--] = nums[left] * nums[left];
|
||||||
|
++left;
|
||||||
|
} else {
|
||||||
|
result[index--] = nums[right] * nums[right];
|
||||||
|
--right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int[] sortedSquares(int[] nums) {
|
||||||
|
int l = 0;
|
||||||
|
int r = nums.length - 1;
|
||||||
|
int[] res = new int[nums.length];
|
||||||
|
int j = nums.length - 1;
|
||||||
|
while(l <= r){
|
||||||
|
if(nums[l] * nums[l] > nums[r] * nums[r]){
|
||||||
|
res[j--] = nums[l] * nums[l++];
|
||||||
|
}else{
|
||||||
|
res[j--] = nums[r] * nums[r--];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```Python
|
```Python
|
||||||
|
@ -133,7 +133,7 @@ class Solution:
|
|||||||
A[i] *= -1
|
A[i] *= -1
|
||||||
K -= 1
|
K -= 1
|
||||||
if K > 0:
|
if K > 0:
|
||||||
A[len(A) - 1] *= ((-1)**K)
|
A[-1] *= (-1)**K #取A最后一个数只需要写-1
|
||||||
return sum(A)
|
return sum(A)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user