Skip to content

Commit b7aa938

Browse files
committed
week04
1 parent e474090 commit b7aa938

4 files changed

Lines changed: 2059 additions & 1 deletion

File tree

Week04/NOTE.md

Lines changed: 200 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,200 @@
1-
学习笔记
1+
## 1 敲代码,看代码,改进,持续改进的过程
2+
3+
### 1.1 生成式 [ ]
4+
5+
[num for num in range(8)]
6+
7+
```python
8+
>>> [[i] for i in range(10)]
9+
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
10+
11+
2、将两个嵌套for循环写成一个列表生成式
12+
如,有一个嵌套列表,a=[[1,2],[3,4],[5,6]],要提取列表里的每一个元素
13+
for循环处理:
14+
for i in a:
15+
for j in i:
16+
print(j)
17+
>>> [j for i in a for j in i] # 先写外循环,后写内循环 !!!
18+
[1, 2, 3, 4, 5, 6]
19+
>>> [j for j in i for i in a]
20+
Traceback (most recent call last):
21+
File "<stdin>", line 1, in <module>
22+
NameError: name 'i' is not defined # 否则报错:
23+
```
24+
25+
​ https://www.cnblogs.com/heyyw1119/p/8711462.html
26+
27+
[ Python基础知识--Slice(切片)和Comprehensions(生成式)](https://www.cnblogs.com/heyyw1119/p/8711462.html)
28+
29+
### 1.2 max, min, sum
30+
31+
​ max_profit = (max_profit, (profit - min_buy_price))
32+
33+
return sum(max(prices[i + 1] - prices[i], 0) for i in range(len(prices) - 1))
34+
35+
### 1.3
36+
37+
```python
38+
elif ten > 0: # 直接写即可
39+
# 收一个20美元,两种找零方法:5+10 和 5*3,优先找大额的
40+
ten -= 1
41+
five -= 1
42+
else:
43+
five -= 3
44+
45+
# elif bill = 20:
46+
# # 收一个20美元,两种找零方法:5+10 和 5*3,优先找大额的
47+
# if ten > 0:
48+
# ten -= 1
49+
# five -= 1
50+
# else:
51+
# five -= 3
52+
53+
```
54+
55+
56+
57+
## 2 错误整理
58+
59+
nums[mid] and mid
60+
61+
62+
63+
## 3 复杂度总结
64+
65+
线性N
66+
67+
跳跃遍历logN
68+
69+
傻递归2^N
70+
71+
排序nlogN
72+
73+
74+
75+
## 4 贪心算法
76+
77+
真实场景:每一步最好,整体不一定最好
78+
79+
因此需要证明:其成立
80+
81+
可以做为辅助算法:最小生成树、
82+
83+
84+
85+
贪心算法:不回退,每一步(子问题)都找到当前状态下,最好或最优的选择,从而得出全局最好或最优的算法
86+
87+
动态规划:回退,保存之前的运算结果,和当前最好的选择,二者比较,择优录取。
88+
89+
90+
91+
贪心:【每一步做出最优判断】
92+
93+
回溯:【回退:清除数据】
94+
95+
动态规划:【每一步做出最优判断 + 回退 = 二者比较,择优录取】
96+
97+
98+
99+
**使用场景:**
100+
101+
从前向后、从后向前
102+
103+
证明可以用贪心
104+
105+
106+
107+
## 5 二分查找
108+
109+
### 模板
110+
111+
```python
112+
# 二分查找
113+
114+
left, right = 0, len(List) - 1
115+
while left <= right: # <=
116+
mid = (left + right)//2
117+
if List[mid] == target:
118+
# find the target
119+
break or return result
120+
elif List[mid] > target:
121+
right = mid -1
122+
else:
123+
left = mid + 1
124+
```
125+
126+
### 适用条件
127+
128+
单调数组(递增或递减) 【否则就只能遍历了】
129+
130+
存在上下界(bounded)【有边界,向中间收缩】
131+
132+
通过索引访问(index accessible )
133+
134+
135+
136+
## 6 BFS DFS
137+
138+
**DFS**用递归、栈
139+
140+
recursion
141+
142+
```python
143+
visited = set()
144+
145+
def dfs(node, visited):
146+
if node in visited: # terminator
147+
# already visited
148+
return
149+
150+
visited.add(node)
151+
152+
# process current node here.
153+
...
154+
for next_node in node.children():
155+
if next_node not in visited:
156+
dfs(next_node, visited)
157+
```
158+
159+
stack
160+
161+
```python
162+
def DFS(self, tree):
163+
if tree.root is None:
164+
return []
165+
166+
visited, stack = [], [tree.root]
167+
168+
while stack:
169+
node = stack.pop()
170+
visited.add(node)
171+
172+
process(node)
173+
nodes = generate_related_nodes(node)
174+
stack.push(nodes)
175+
176+
# other processing work
177+
...
178+
```
179+
180+
**BFS:**用队列
181+
182+
```python
183+
# BFS
184+
def BFS(graph, start, end):
185+
visited = set()
186+
queue = []
187+
queue.append([start])
188+
189+
while queue:
190+
node = queue.pop()
191+
visited.add(node)
192+
193+
processs(node)
194+
nodes = generate_related_nodes(node)
195+
queue.push(nodes)
196+
197+
# other processing word
198+
...
199+
```
200+

0 commit comments

Comments
 (0)