Skip to content

Commit 9ad390b

Browse files
committed
Chapter 09 section 01 C++ codes updated. Java codes added.
1 parent 922dc27 commit 9ad390b

File tree

7 files changed

+149
-24
lines changed

7 files changed

+149
-24
lines changed

09-Dynamic-Programming/Course Code (C++)/01-Fibonacci/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using namespace std;
55

66
int num = 0;
77

8+
// 递归求斐波那契数列
89
int fib( int n ){
910

1011
num ++;
@@ -27,9 +28,9 @@ int main() {
2728
int res = fib(n);
2829
time_t endTime = clock();
2930

30-
cout<<"fib("<<n<<") = "<<res<<endl;
31-
cout<<"time : "<<double(endTime-startTime)/CLOCKS_PER_SEC<<" s"<<endl;
32-
cout<<"run function fib() "<<num<<"times."<<endl;
31+
cout << "fib(" << n << ") = " << res << endl;
32+
cout << "time : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
33+
cout << "run function fib() " << num << " times." << endl;
3334

3435
return 0;
3536
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#include <iostream>
22
#include <ctime>
33
#include <vector>
4+
45
using namespace std;
56

67
vector<int> memo;
78
int num = 0;
89

910
// 记忆化搜索
10-
int fib( int n ){
11+
int fib(int n){
1112

1213
num ++;
1314

14-
if( n == 0 )
15+
if(n == 0)
1516
return 0;
1617

17-
if( n == 1 )
18+
if(n == 1)
1819
return 1;
1920

20-
if( memo[n] == -1 )
21-
memo[n] = fib(n-1) + fib(n-2);
21+
if(memo[n] == -1)
22+
memo[n] = fib(n - 1) + fib(n - 2);
2223

2324
return memo[n];
2425
}
@@ -28,16 +29,17 @@ int main() {
2829
num = 0;
2930

3031
//int n = 42;
31-
int n = 1000;
32-
memo = vector<int>(n+1,-1);
32+
int n = 1000; // 注意: 我们使用n = 1000只是为了测试性能, 实际上会溢出
33+
// 斐波那契额数列是以指数速度上涨的
34+
memo = vector<int>(n + 1, -1);
3335

3436
time_t startTime = clock();
3537
int res = fib(n);
3638
time_t endTime = clock();
3739

38-
cout<<"fib("<<n<<") = "<<res<<endl;
39-
cout<<"time : "<<double(endTime-startTime)/CLOCKS_PER_SEC<<" s"<<endl;
40-
cout<<"run function fib() "<<num<<"times."<<endl;
40+
cout << "fib(" << n << ") = " << res << endl;
41+
cout << "time : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
42+
cout << "run function fib() " << num << "times." << endl;
4143

4244
return 0;
4345
}

09-Dynamic-Programming/Course Code (C++)/01-Fibonacci/main3.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@ using namespace std;
66
// 动态规划
77
int fib( int n ){
88

9-
vector<int> memo(n+1, -1);
9+
vector<int> memo(n + 1, -1);
1010

1111
memo[0] = 0;
1212
memo[1] = 1;
13-
for( int i = 2 ; i <= n ; i ++ )
14-
memo[i] = memo[i-1] + memo[i-2];
13+
for(int i = 2 ; i <= n ; i ++)
14+
memo[i] = memo[i - 1] + memo[i - 2];
1515

1616
return memo[n];
1717
}
1818

1919
int main() {
2020

21-
int n = 1000;
21+
// int n = 42
22+
int n = 1000; // 注意: 我们使用n = 1000只是为了测试性能, 实际上会溢出
23+
// 斐波那契额数列是以指数速度上涨的
2224

2325
time_t startTime = clock();
2426
int res = fib(n);
2527
time_t endTime = clock();
2628

27-
cout<<"fib("<<n<<") = "<<res<<endl;
28-
cout<<"time : "<<double(endTime-startTime)/CLOCKS_PER_SEC<<" s"<<endl;
29+
cout << "fib(" << n << ") = " << res << endl;
30+
cout << "time : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
2931

3032
return 0;
3133
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 递归求斐波那契数列
2+
public class Solution1 {
3+
4+
private int num = 0;
5+
6+
public int fib( int n ){
7+
8+
num ++;
9+
10+
if( n == 0 )
11+
return 0;
12+
13+
if( n == 1 )
14+
return 1;
15+
16+
return fib(n-1) + fib(n-2);
17+
}
18+
19+
public int getNum(){
20+
return num;
21+
}
22+
23+
public static void main(String[] args) {
24+
25+
int n = 42;
26+
27+
Solution1 solution = new Solution1();
28+
long startTime = System.currentTimeMillis();
29+
int res = solution.fib(n);
30+
long endTime = System.currentTimeMillis();
31+
32+
System.out.println("fib(" + n + ") = " + res);
33+
System.out.println("time : " + (endTime - startTime) + " ms");
34+
System.out.println("run function fib() " + solution.getNum() + " times.");
35+
}
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Arrays;
2+
3+
// 记忆化搜索
4+
public class Solution2 {
5+
6+
private int num = 0;
7+
8+
public int fib(int n){
9+
10+
int[] memo = new int[n + 1];
11+
Arrays.fill(memo, -1);
12+
return fib(n, memo);
13+
}
14+
15+
private int fib(int n, int[] memo){
16+
17+
num ++;
18+
19+
if(n == 0)
20+
return 0;
21+
22+
if(n == 1)
23+
return 1;
24+
25+
if(memo[n] == -1)
26+
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
27+
28+
return memo[n];
29+
}
30+
31+
public int getNum(){
32+
return num;
33+
}
34+
35+
public static void main(String[] args) {
36+
37+
//int n = 42;
38+
int n = 1000; // 注意: 我们使用n = 1000只是为了测试性能, 实际上会溢出
39+
// 斐波那契额数列是以指数速度上涨的
40+
41+
Solution2 solution = new Solution2();
42+
long startTime = System.currentTimeMillis();
43+
int res = solution.fib(n);
44+
long endTime = System.currentTimeMillis();
45+
46+
System.out.println("fib(" + n + ") = " + res);
47+
System.out.println("time : " + (endTime - startTime) + " ms");
48+
System.out.println("run function fib() " + solution.getNum() + " times.");
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Arrays;
2+
3+
// 动态规划
4+
public class Solution3 {
5+
6+
public int fib(int n){
7+
8+
int[] memo = new int[n + 1];
9+
Arrays.fill(memo, -1);
10+
11+
memo[0] = 0;
12+
memo[1] = 1;
13+
for(int i = 2 ; i <= n ; i ++)
14+
memo[i] = memo[i - 1] + memo[i - 2];
15+
16+
return memo[n];
17+
}
18+
19+
public static void main(String[] args) {
20+
21+
//int n = 42;
22+
int n = 1000; // 注意: 我们使用n = 1000只是为了测试性能, 实际上会溢出
23+
// 斐波那契额数列是以指数速度上涨的
24+
25+
Solution3 solution = new Solution3();
26+
long startTime = System.currentTimeMillis();
27+
int res = solution.fib(n);
28+
long endTime = System.currentTimeMillis();
29+
30+
System.out.println("fib(" + n + ") = " + res);
31+
System.out.println("time : " + (endTime - startTime) + " ms");
32+
}
33+
}

readme.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@
9999
| 8-1 树形问题 Letter Combinations of a Phone Number | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/01-02-Letter-Combinations-of-a-Phone-Number/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/01-02-Letter-Combinations-of-a-Phone-Number/src/) |
100100
| 8-2 什么是回溯 | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/01-02-Letter-Combinations-of-a-Phone-Number/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/01-02-Letter-Combinations-of-a-Phone-Number/src/) |
101101
| 8-3 排列问题 Permutations | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/03-Permutations/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/03-Permutations/src/) |
102-
| 8-4 组合问题 Combinations | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/04-Combinations) | [Java源码] |
103-
| 8-5 回溯法解决组合问题的优化 | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/05-Combinations-optimized) | [Java源码] |
104-
| 8-6 二维平面上的回溯法 Word Search | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/06-Word-Search) | [Java源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(Java)/06-Word-Search/src) |
105-
| 8-7 floodfill算法,一类经典问题 Number of Islands | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/07-Number-of-Islands) | [Java源码] |
106-
| 8-8 回溯法是人工智能的基础 N Queens | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/08-N-Queens) | [Java源码] |
102+
| 8-4 组合问题 Combinations | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/04-Combinations/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/04-Combinations/src/) |
103+
| 8-5 回溯法解决组合问题的优化 | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/05-Combinations-optimized/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/05-Combinations-optimized/src/) |
104+
| 8-6 二维平面上的回溯法 Word Search | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/06-Word-Search/) | [Java源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(Java)/06-Word-Search/src/) |
105+
| 8-7 floodfill算法,一类经典问题 Number of Islands | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/07-Number-of-Islands/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/07-Number-of-Islands/src/) |
106+
| 8-8 回溯法是人工智能的基础 N Queens | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/08-Recurion-and-Backstracking/Course%20Code%20(C%2B%2B)/08-N-Queens/) | [Java源码](08-Recurion-and-Backstracking/Course%20Code%20(Java)/08-N-Queens/src/) |
107107
| 补充代码1:更多和生成排列相关 | [整理中] | [敬请期待] |
108108
| 补充代码2:更多和组合相关 | [整理中] | [敬请期待] |
109+
| 补充代码3:更多和八皇后问题相关 | [整理中] | [敬请期待] |
109110
| **第九章:动态规划基础** | [章节C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/09-Dynamic-Programming/Course%20Code%20(C%2B%2B)) | [章节Java源码] |
110111
| 9-1 什么是动态规划 | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/09-Dynamic-Programming/Course%20Code%20(C%2B%2B)/01-Fibonacci) | [Java源码] |
111112
| 9-2 第一个动态规划问题 Climbing Stairs | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/09-Dynamic-Programming/Course%20Code%20(C%2B%2B)/02-Climbing-Stairs) | [Java源码] |

0 commit comments

Comments
 (0)