111 · 爬楼梯Climbing Stairs
描述
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
样例
Example 1:
Input:
n = 3
Output:
3
Explanation:
1, 1, 1
1, 2
2, 1
total 3.
public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int climbStairs(int n) {
// write your code here
if(n <= 1){
return n ;
}
int last = 1 , lastlast = 1 ;
int now = 0 ;
for(int i = 2 ; i <= n ; i++ ){
now = last + lastlast ;
lastlast = last ;
last = now ;
}
return now ;
}
}