Wednesday, October 21, 2015

[leetcode] Climbing Stairs

每一步 都是 靠 前一步 和 前两步 决定的
currentstep = previousstep+previousTwoSteps
这样直接bottom up 上去就好了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Solution {
    public int climbStairs(int n) {
        if (n <= 1){
            return 1;
        }
        int i_1 = 1;
        int i_2 = 1;
        int result = 0;
        for (int i = 2; i <= n; i++){
            result = i_1 + i_2;
            i_2 = i_1;
            i_1 = result;
        }
        
        return result;
    }
}

No comments:

Post a Comment