美文网首页动态规划
Sequence DP - 70. Climbing Stair

Sequence DP - 70. Climbing Stair

作者: Super_Alan | 来源:发表于2018-04-06 10:34 被阅读0次

    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 Stair

        本文链接:https://www.haomeiwen.com/subject/wqyhhftx.html