LeetCode-Climbing Stairs

答案和fibnacci数列一样,最简单的dp

首先不要用一个array存,只存当前两个数就可以

其次注意边界条件 n<=0 n=1 n=2

public class Solution {
    public int climbStairs(int n) {
        int first = 1;
        int second = 1;
        for ( int i = 2; i <= n; i ++){
            int cur = first + second;
            first = second;
            second = cur;
        }
        return second;
    }
}


你可能感兴趣的:(LeetCode-Climbing Stairs)