forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbStairs.java
More file actions
44 lines (40 loc) · 856 Bytes
/
ClimbStairs.java
File metadata and controls
44 lines (40 loc) · 856 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
package normal;
/**
* @program JavaBooks
* @description: 70. 爬楼梯
* @author: mf
* @create: 2019/11/10 12:52
*/
/*
题目:https://leetcode-cn.com/problems/climbing-stairs/
难度:easy
类型:动态规划
*/
/*
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
*/
public class ClimbStairs {
public static void main(String[] args) {
System.out.println(climbStairs(3));
}
private static int climbStairs(int n) {
if (n <= 2) return n;
int pre2 = 1, pre1 = 2;
for (int i = 3; i <= n; i++) {
int cur = pre1 + pre2;
pre2 = pre1;
pre1 = cur;
}
return pre1;
}
}