Skip to content

Commit de0f4dd

Browse files
committed
update
1 parent 03a6f02 commit de0f4dd

5 files changed

+120
-0
lines changed

72. 背包问题.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
***我们有一个背包,背包总的承载重量是 Wkg。现在我们有 n 个物品,每个物品的重量不等,并且不可分割。我们现在期望选择几件物品,装载到背包中。在不超过背包所能装载重量的前提下,如何让背包中物品的总重量最大?***
2+
3+
```
4+
class Solution:
5+
def bag(self, items: List[int], capacity:int) -> List[List[int]]:
6+
ind = 0
7+
cur = 0
8+
max_weight = 0
9+
n = len(items)
10+
#构造递归
11+
def dfs(ind, cur):
12+
#递归终止条件
13+
if ind == n:
14+
if cur > max_weight:
15+
max_weight = cur
16+
return
17+
#情况1
18+
if cur+items[ind] <= capacity:
19+
dfs(ind+1, cur+items[ind])
20+
#情况2
21+
dfs(ind+1, cur)
22+
#调用递归
23+
dfs(ind, cur)
24+
#返回要优化的目标
25+
return max_weight
26+
```

73. 每日温度.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
***请根据每日气温列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。***
2+
3+
```
4+
例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
5+
```
6+
7+
```
8+
class Solution(object):
9+
def dailyTemperatures(self, temperatures):
10+
"""
11+
:type temperatures: List[int]
12+
:rtype: List[int]
13+
"""
14+
#思路:递减栈,遍历整个数组,如果栈不空,且当前数字大于栈顶元素,取出栈顶元素,直接求出下标差就是二者的距离,继续看新的栈顶元素,直到当前数字小于等于栈顶元素停止,然后将数字对应下标入栈,
15+
n = len(temperatures)
16+
res = [0]*n
17+
stack = []
18+
19+
for i in range(n):
20+
while (stack and temperatures[i] > temperatures[stack[-1]]):
21+
ind = stack.pop()
22+
res[ind] = i - ind
23+
stack.append(i)
24+
return res
25+
```

74. 有效的括号.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
***给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。***
2+
3+
```
4+
class Solution:
5+
def isValid(self, s: str) -> bool:
6+
hashmap = {'(':')','[':']','{':'}'}
7+
stack = []
8+
9+
for c in s:
10+
if c in hashmap:
11+
stack.append(c)
12+
else:
13+
if stack:
14+
top = stack[-1]
15+
if c != hashmap[top]:
16+
return False
17+
else:
18+
stack.pop()
19+
else:
20+
return False
21+
return not stack
22+
```

75. 字符串解码.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
***输入:s = "3[a]2[bc]" 输出:"aaabcbc"***
2+
3+
```
4+
class Solution(object):
5+
def decodeString(self, s):
6+
"""
7+
:type s: str
8+
:rtype: str
9+
"""
10+
stack=[]
11+
res = ''
12+
multi = 0
13+
14+
for c in s:
15+
if '0' <= c <= '9':
16+
multi = 10*multi+int(c)
17+
elif c == '[':
18+
stack.append((res, multi))
19+
res = ''
20+
multi = 0
21+
elif c == ']':
22+
last_res, last_multi = stack.pop()
23+
res = last_res+res*last_multi
24+
else:
25+
res += c
26+
return res
27+
```

76. 栈的压入、弹出序列.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
***输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。***
2+
3+
```
4+
初始化: 辅助栈 stack,弹出序列的索引 i;
5+
遍历压栈序列: 各元素记为 num;
6+
元素 num 入栈;
7+
循环出栈:若 stack 的栈顶元素 == 弹出序列元素 popped[i],则执行出栈与 i++;
8+
返回值: 若 stack 为空,则此弹出序列合法。
9+
10+
class Solution:
11+
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
12+
stack = []
13+
i = 0
14+
for num in pushed:
15+
stack.append(num)
16+
while stack and stack[-1] == popped[i]:
17+
stack.pop()
18+
i += 1
19+
return not stack
20+
```

0 commit comments

Comments
 (0)