Sequence DP - 70. Climbing Stairs

https://leetcode.com/problems/climbing-stairs/description/

Climbing Stairs
state: f[i]表示前i个位置,跳到第i个位置的方案 总数
function: f[i] = f[i-1] + f[i-2] intialize: f[0] = 1
answer: f[n]

class Solution {
    public int climbStairs(int n) {
        if (n <= 0) {
            return 0;
        } else if (n <= 2) {
            return n;
        }
        
        int[] ways = new int[n];
        ways[0] = 1;
        ways[1] = 2;
        
        for (int i = 2; i < n; i++) {
            ways[i] = ways[i - 1] + ways[i - 2];
        }

        return ways[n - 1];
    }
}

你可能感兴趣的:(Sequence DP - 70. Climbing Stairs)