Saturday, November 14, 2015

[leetcode]Climbing Stairs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Solution {
    public int climbStairs(int n) {
        if (n < 0) return 0;
        if (n == 0 || n == 1) return 1;
        
        int steps[] = new int[n+1];
        steps[1] = 1;
        steps[2] = 2;
        for(int i = 3; i <= n; i++){
            steps[i] = steps[i-1]+steps[i-2];
        }
        
        return steps[n];
    }
}

No comments:

Post a Comment