-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0070.爬楼梯.java
68 lines (65 loc) · 1.12 KB
/
0070.爬楼梯.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode.cn id=70 lang=java
*
* [70] 爬楼梯
*
* https://leetcode.cn/problems/climbing-stairs/description/
*
* algorithms
* Easy (53.98%)
* Likes: 3104
* Dislikes: 0
* Total Accepted: 1.2M
* Total Submissions: 2.2M
* Testcase Example: '2'
*
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
*
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
*
*
*
* 示例 1:
*
*
* 输入:n = 2
* 输出:2
* 解释:有两种方法可以爬到楼顶。
* 1. 1 阶 + 1 阶
* 2. 2 阶
*
* 示例 2:
*
*
* 输入:n = 3
* 输出:3
* 解释:有三种方法可以爬到楼顶。
* 1. 1 阶 + 1 阶 + 1 阶
* 2. 1 阶 + 2 阶
* 3. 2 阶 + 1 阶
*
*
*
*
* 提示:
*
*
* 1 <= n <= 45
*
*
*/
// @lc code=start
class Solution {
public int climbStairs(int n) {
if (n <= 2)
return n;
int p = 1, q = 2, ans = 0;
for (int i = 3; i <= n; i++) {
ans = p + q;
p = q;
q = ans;
}
return ans;
}
}
// @lc code=end