70.Climbing Stairs

来源:互联网 发布:淘宝企业c店开店流程 编辑:程序博客网 时间:2024/04/25 22:23

题目链接:climbing-stairs


/** * You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? * */public class ClimbingStairs {//45 / 45 test cases passed.//Status: Accepted//Runtime: 183 ms//Submitted: 0 minutes ago//解题思路:要爬到第n阶楼梯,有两种情况://一、 从第(n - 1)阶出发 一次一步到//二、 从第(n - 2)阶出发 一次两步到//故就是以上两种情况之和,为斐波那契数列    static int climbStairs(int n) {        if(n == 0) return 0;        if(n == 1) return 1;            int[] count = new int[n + 1];            count[0] = 0;        count[1] = 1;        count[2] = 2;                 for (int i = 3; i <= n; i++) {count[i] = count[i - 1] + count[i - 2];}        return count[n];    }public static void main(String[] args) {System.out.println(climbStairs(5));}}


0 0
原创粉丝点击