forked from algorithm010/algorithm010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive.php
More file actions
47 lines (40 loc) · 975 Bytes
/
recursive.php
File metadata and controls
47 lines (40 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 1. 避免人肉递归
* 2. 找最近重复子问题
* 3. 数学归纳法(n=1成立,n=2成立,且当n成立时n+1也能成立)
/**
//PHP
public function recur($level, $param) {
//终结条件
if ($level > MAX_LEVEL) {
return ;
}
//处理当前层逻辑
//进入下一层
$this->recur($level + 1, $newParam);
//清理当前层(可选)
}
// Java
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
}
# Python
def recursion(level, param1, param2, ...):
# recursion terminator
if level > MAX_LEVEL:
process_result
return
# process logic in current level
process(level, data...)
# drill down
self.recursion(level + 1, p1, ...)
# reverse the current level status if needed