Skip to content

Commit 66253b9

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 71ee2d5 + aacb26d commit 66253b9

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

problems/0042.接雨水.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,44 @@ class Solution:
388388
res += res1
389389
return res
390390
```
391+
动态规划
392+
```python3
393+
class Solution:
394+
def trap(self, height: List[int]) -> int:
395+
leftheight, rightheight = [0]*len(height), [0]*len(height)
396+
397+
leftheight[0]=height[0]
398+
for i in range(1,len(height)):
399+
leftheight[i]=max(leftheight[i-1],height[i])
400+
rightheight[-1]=height[-1]
401+
for i in range(len(height)-2,-1,-1):
402+
rightheight[i]=max(rightheight[i+1],height[i])
403+
404+
result = 0
405+
for i in range(0,len(height)):
406+
summ = min(leftheight[i],rightheight[i])-height[i]
407+
result += summ
408+
return result
409+
```
410+
单调栈
411+
```python3
412+
class Solution:
413+
def trap(self, height: List[int]) -> int:
414+
st =[0]
415+
result = 0
416+
for i in range(1,len(height)):
417+
while st!=[] and height[i]>height[st[-1]]:
418+
midh = height[st[-1]]
419+
st.pop()
420+
if st!=[]:
421+
hright = height[i]
422+
hleft = height[st[-1]]
423+
h = min(hright,hleft)-midh
424+
w = i-st[-1]-1
425+
result+=h*w
426+
st.append(i)
427+
return result
428+
```
391429

392430
Go:
393431

problems/0084.柱状图中最大的矩形.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,57 @@ public:
191191

192192
这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节!
193193

194+
## 其他语言版本
195+
196+
Java:
197+
198+
Python:
199+
200+
动态规划
201+
```python3
202+
class Solution:
203+
def largestRectangleArea(self, heights: List[int]) -> int:
204+
result = 0
205+
minleftindex, minrightindex = [0]*len(heights), [0]*len(heights)
206+
207+
minleftindex[0]=-1
208+
for i in range(1,len(heights)):
209+
t = i-1
210+
while t>=0 and heights[t]>=heights[i]: t=minleftindex[t]
211+
minleftindex[i]=t
212+
213+
minrightindex[-1]=len(heights)
214+
for i in range(len(heights)-2,-1,-1):
215+
t=i+1
216+
while t<len(heights) and heights[t]>=heights[i]: t=minrightindex[t]
217+
minrightindex[i]=t
218+
219+
for i in range(0,len(heights)):
220+
left = minleftindex[i]
221+
right = minrightindex[i]
222+
summ = (right-left-1)*heights[i]
223+
result = max(result,summ)
224+
return result
225+
```
226+
单调栈 版本二
227+
```python3
228+
class Solution:
229+
def largestRectangleArea(self, heights: List[int]) -> int:
230+
heights.insert(0,0) # 数组头部加入元素0
231+
heights.append(0) # 数组尾部加入元素0
232+
st = [0]
233+
result = 0
234+
for i in range(1,len(heights)):
235+
while st!=[] and heights[i]<heights[st[-1]]:
236+
midh = heights[st[-1]]
237+
st.pop()
238+
if st!=[]:
239+
minrightindex = i
240+
minleftindex = st[-1]
241+
summ = (minrightindex-minleftindex-1)*midh
242+
result = max(summ,result)
243+
st.append(i)
244+
return result
245+
```
246+
194247
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

problems/0102.二叉树的层序遍历.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,35 @@ public:
12051205
};
12061206
```
12071207

1208+
java代码:
1209+
1210+
```java
1211+
class Solution {
1212+
public Node connect(Node root) {
1213+
Queue<Node> tmpQueue = new LinkedList<Node>();
1214+
if (root != null) tmpQueue.add(root);
1215+
1216+
while (tmpQueue.size() != 0){
1217+
int size = tmpQueue.size();
1218+
1219+
Node cur = tmpQueue.poll();
1220+
if (cur.left != null) tmpQueue.add(cur.left);
1221+
if (cur.right != null) tmpQueue.add(cur.right);
1222+
1223+
for (int index = 1; index < size; index++){
1224+
Node next = tmpQueue.poll();
1225+
if (next.left != null) tmpQueue.add(next.left);
1226+
if (next.right != null) tmpQueue.add(next.right);
1227+
1228+
cur.next = next;
1229+
cur = next;
1230+
}
1231+
}
1232+
1233+
return root;
1234+
}
1235+
}
1236+
```
12081237

12091238
python代码:
12101239

problems/剑指Offer58-II.左旋转字符串.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ class Solution:
141141
# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改
142142
```
143143

144+
```python 3
145+
#方法三:考虑不能用切片的情况下,利用模+下标实现
146+
class Solution:
147+
def reverseLeftWords(self, s: str, n: int) -> str:
148+
new_s = ''
149+
for i in range(len(s)):
150+
j = (i+n)%len(s)
151+
new_s = new_s + s[j]
152+
return new_s
153+
154+
```
155+
144156
Go:
145157

146158
```go

0 commit comments

Comments
 (0)