Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc0c878

Browse files
更新 0070.爬楼梯.md Java代码
三种Java代码中有两种是相同的方法,合并相同方法的内容
1 parent 73d7e3f commit cc0c878

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

‎problems/0070.爬楼梯.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,6 @@ public:
213213
214214
215215
### Java
216-
```Java
217-
class Solution {
218-
public int climbStairs(int n) {
219-
// 跟斐波那契数列一样
220-
if(n <= 2) return n;
221-
int a = 1, b = 2, sum = 0;
222-
223-
for(int i = 3; i <= n; i++){
224-
sum = a + b;
225-
a = b;
226-
b = sum;
227-
}
228-
return b;
229-
}
230-
}
231-
```
232216
233217
```java
234218
// 常规方式
@@ -241,15 +225,22 @@ public int climbStairs(int n) {
241225
}
242226
return dp[n];
243227
}
228+
```
229+
230+
```Java
244231
// 用变量记录代替数组
245-
public int climbStairs(int n) {
246-
int a = 0, b = 1, c = 0; // 默认需要1次
247-
for (int i = 1; i <= n; i++) {
248-
c = a + b; // f(i - 1) + f(n - 2)
249-
a = b; // 记录上一轮的值
250-
b = c; // 向后步进1个数
232+
class Solution {
233+
public int climbStairs(int n) {
234+
if(n <= 2) return n;
235+
int a = 1, b = 2, sum = 0;
236+
237+
for(int i = 3; i <= n; i++){
238+
sum = a + b; // f(i - 1) + f(i - 2)
239+
a = b; // 记录f(i - 1),即下一轮的f(i - 2)
240+
b = sum; // 记录f(i),即下一轮的f(i - 1)
241+
}
242+
return b;
251243
}
252-
return c;
253244
}
254245
```
255246

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /