算法训练营总结
关键点
精通一个领域
切碎知识点
刻意练习
反馈
算法的学习方法
不要死磕
五毒神掌
要看高票代码(反馈)
最大误区
题目只做一遍
递归
不要人肉进行递归(最大误区)
找到最近最简方法,将其拆解成可重复解决的问题(重复子问题)
数学归纳法思维
动态规划
打破自己的思维惯性,形成机器思维
理解复杂逻辑的关键
也是职业进阶的要点要领
课程内容总结
代码模板
分治
def divide_conquer(problem, param1, param2, ...):
if problem is None:
print_result
return
data = prepare_data(problem)
subproblems = split_problem(problem, data)
subresult1 = self.divide_conquer(subproblems[0], p1, ...)
subresult2 = self.divide_conquer(subproblems[1], p1, ...)
subresult3 = self.divide_conquer(subproblems[2], p1, ...)
...
result = process_result(subresult1, subresult2, subresult3, …)
DFS 代码 - 递归写法
visited = set()
def dfs(node, visited):
if node in visited: # terminator
return
visited.add(node)
...
for next_node in node.children():
if not next_node in visited:
dfs(next node, visited)
DFS 代码 - 非递归写法
def DFS(self, tree):
if tree.root is None:
return []
visited, stack = [], [tree.root]
while stack:
node = stack.pop()
visited.add(node)
process (node)
nodes = generaterelatednodes(node)
stack.push(nodes)
BFS 代码
def BFS(graph, start, end):
queue = []
queue.append([start])
visited.add(start)
while queue:
node = queue.pop()
visited.add(node)
process(node)
nodes = generaterelatednodes(node)
queue.push(nodes)
二分查找代码模板
left, right = 0, len(array) - 1
while left <= right:
mid = (left + right) / 2
if array[mid] == target:
break or return result
elif array[mid] < target:
left = mid + 1
else:
right = mid - 1
递归代码模版
public void recur(int level, int param) {
// terminator
if (level > MAX_LEVEL) {
// process result
return;
}
// process current logic
process(level, param);
// drill down
recur( level: level + 1, newParam);
// restore current status
}
评论