mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -388,6 +388,44 @@ class Solution:
|
||||
res += res1
|
||||
return res
|
||||
```
|
||||
动态规划
|
||||
```python3
|
||||
class Solution:
|
||||
def trap(self, height: List[int]) -> int:
|
||||
leftheight, rightheight = [0]*len(height), [0]*len(height)
|
||||
|
||||
leftheight[0]=height[0]
|
||||
for i in range(1,len(height)):
|
||||
leftheight[i]=max(leftheight[i-1],height[i])
|
||||
rightheight[-1]=height[-1]
|
||||
for i in range(len(height)-2,-1,-1):
|
||||
rightheight[i]=max(rightheight[i+1],height[i])
|
||||
|
||||
result = 0
|
||||
for i in range(0,len(height)):
|
||||
summ = min(leftheight[i],rightheight[i])-height[i]
|
||||
result += summ
|
||||
return result
|
||||
```
|
||||
单调栈
|
||||
```python3
|
||||
class Solution:
|
||||
def trap(self, height: List[int]) -> int:
|
||||
st =[0]
|
||||
result = 0
|
||||
for i in range(1,len(height)):
|
||||
while st!=[] and height[i]>height[st[-1]]:
|
||||
midh = height[st[-1]]
|
||||
st.pop()
|
||||
if st!=[]:
|
||||
hright = height[i]
|
||||
hleft = height[st[-1]]
|
||||
h = min(hright,hleft)-midh
|
||||
w = i-st[-1]-1
|
||||
result+=h*w
|
||||
st.append(i)
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -91,18 +91,18 @@ if (word1[i - 1] != word2[j - 1])
|
||||
|
||||
`if (word1[i - 1] != word2[j - 1])`,此时就需要编辑了,如何编辑呢?
|
||||
|
||||
* 操作一:word1增加一个元素,使其word1[i - 1]与word2[j - 1]相同,那么就是以下标i-2为结尾的word1 与 j-1为结尾的word2的最近编辑距离 加上一个增加元素的操作。
|
||||
* 操作一:word1删除一个元素,那么就是以下标i - 2为结尾的word1 与 j-1为结尾的word2的最近编辑距离 再加上一个操作。
|
||||
|
||||
即 `dp[i][j] = dp[i - 1][j] + 1;`
|
||||
|
||||
|
||||
* 操作二:word2添加一个元素,使其word1[i - 1]与word2[j - 1]相同,那么就是以下标i-1为结尾的word1 与 j-2为结尾的word2的最近编辑距离 加上一个增加元素的操作。
|
||||
* 操作二:word2删除一个元素,那么就是以下标i - 1为结尾的word1 与 j-2为结尾的word2的最近编辑距离 再加上一个操作。
|
||||
|
||||
即 `dp[i][j] = dp[i][j - 1] + 1;`
|
||||
|
||||
这里有同学发现了,怎么都是添加元素,删除元素去哪了。
|
||||
这里有同学发现了,怎么都是删除元素,添加元素去哪了。
|
||||
|
||||
**word2添加一个元素,相当于word1删除一个元素**,例如 `word1 = "ad" ,word2 = "a"`,`word1`删除元素`'d'`,`word2`添加一个元素`'d'`,变成`word1="a", word2="ad"`, 最终的操作数是一样! dp数组如下图所示意的:
|
||||
**word2添加一个元素,相当于word1删除一个元素**,例如 `word1 = "ad" ,word2 = "a"`,`word1`删除元素`'d'` 和 `word2`添加一个元素`'d'`,变成`word1="a", word2="ad"`, 最终的操作数是一样! dp数组如下图所示意的:
|
||||
|
||||
```
|
||||
a a d
|
||||
|
@ -191,4 +191,57 @@ public:
|
||||
|
||||
这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节!
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
|
||||
Python:
|
||||
|
||||
动态规划
|
||||
```python3
|
||||
class Solution:
|
||||
def largestRectangleArea(self, heights: List[int]) -> int:
|
||||
result = 0
|
||||
minleftindex, minrightindex = [0]*len(heights), [0]*len(heights)
|
||||
|
||||
minleftindex[0]=-1
|
||||
for i in range(1,len(heights)):
|
||||
t = i-1
|
||||
while t>=0 and heights[t]>=heights[i]: t=minleftindex[t]
|
||||
minleftindex[i]=t
|
||||
|
||||
minrightindex[-1]=len(heights)
|
||||
for i in range(len(heights)-2,-1,-1):
|
||||
t=i+1
|
||||
while t<len(heights) and heights[t]>=heights[i]: t=minrightindex[t]
|
||||
minrightindex[i]=t
|
||||
|
||||
for i in range(0,len(heights)):
|
||||
left = minleftindex[i]
|
||||
right = minrightindex[i]
|
||||
summ = (right-left-1)*heights[i]
|
||||
result = max(result,summ)
|
||||
return result
|
||||
```
|
||||
单调栈 版本二
|
||||
```python3
|
||||
class Solution:
|
||||
def largestRectangleArea(self, heights: List[int]) -> int:
|
||||
heights.insert(0,0) # 数组头部加入元素0
|
||||
heights.append(0) # 数组尾部加入元素0
|
||||
st = [0]
|
||||
result = 0
|
||||
for i in range(1,len(heights)):
|
||||
while st!=[] and heights[i]<heights[st[-1]]:
|
||||
midh = heights[st[-1]]
|
||||
st.pop()
|
||||
if st!=[]:
|
||||
minrightindex = i
|
||||
minleftindex = st[-1]
|
||||
summ = (minrightindex-minleftindex-1)*midh
|
||||
result = max(summ,result)
|
||||
st.append(i)
|
||||
return result
|
||||
```
|
||||
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>
|
||||
|
@ -1205,6 +1205,35 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
java代码:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public Node connect(Node root) {
|
||||
Queue<Node> tmpQueue = new LinkedList<Node>();
|
||||
if (root != null) tmpQueue.add(root);
|
||||
|
||||
while (tmpQueue.size() != 0){
|
||||
int size = tmpQueue.size();
|
||||
|
||||
Node cur = tmpQueue.poll();
|
||||
if (cur.left != null) tmpQueue.add(cur.left);
|
||||
if (cur.right != null) tmpQueue.add(cur.right);
|
||||
|
||||
for (int index = 1; index < size; index++){
|
||||
Node next = tmpQueue.poll();
|
||||
if (next.left != null) tmpQueue.add(next.left);
|
||||
if (next.right != null) tmpQueue.add(next.right);
|
||||
|
||||
cur.next = next;
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
python代码:
|
||||
|
||||
|
@ -141,6 +141,18 @@ class Solution:
|
||||
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
|
||||
```
|
||||
|
||||
```python 3
|
||||
#方法三:考虑不能用切片的情况下,利用模+下标实现
|
||||
class Solution:
|
||||
def reverseLeftWords(self, s: str, n: int) -> str:
|
||||
new_s = ''
|
||||
for i in range(len(s)):
|
||||
j = (i+n)%len(s)
|
||||
new_s = new_s + s[j]
|
||||
return new_s
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
|
Reference in New Issue
Block a user