[LeetCode] NO. 70 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?

[题目解析] 这是一道比较经典的题目,我们可以考虑最后到达第n个台阶的前一步,有两种情况:一种是在第n-1个台阶,爬1个台阶到达;另外一种是在第n-2个台阶上,爬2个台阶到达。用函数f(n)表示该函数,则可以表示成f(n) = f(n-1) + f(n-2),其中n > 2。显然该问题也是求第n个斐波那契数的问题。递归方法如下。

   public int climbStairs(int n) {
        if(n >= 2){
        	return climbStairs(n-1) + climbStairs(n-2);
        }
        return 1;
   }

  LeetCode上提交该方法,出现Time Limit Exceeded异常,递归显然是费时的。我们可以考虑用一个结构来保存中间结果,下次用到中间结果的时候不用重新计算,或者用循环的方法来解决。思路如下。

   public int climbStairs(int n) {
        int a = 1,b=1,ret=0;
        if(n==1 || n==2) return n;
        for(int i = 2; i <= n; i++){
            ret = a + b;
            a = b;
            b = ret;
        }
        return ret;
   }

  

时间: 2024-12-16 14:56:39

[LeetCode] NO. 70 Climbing Stairs的相关文章

【一天一道LeetCode】#70. Climbing Stairs

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【LeetCode】70 - 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? Hide Tags: Dynamic Programming Solution:一个台阶的方法次数为1次,两个台阶的方法次数为2个.n个台阶的方法可以理解成上n-2个台

leetCode 70. Climbing Stairs | 动态规划

70. 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? 思考: top steps 1 1 2 2 3 3 4 5 5 8 6 13 ... ... 从上面的分析可以看出,f(top)

377. Combination Sum IV 70. Climbing Stairs

back function (return number) remember the structure class Solution { int res = 0; //List<List<Integer>> resList = new ArrayList<List<Integer>>(); public int combinationSum4(int[] nums, int target) { Arrays.sort(nums); return back(

刷题70. Climbing Stairs

一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方法计算: class Solution{ public: int climbStairs(int n){ if(n==1) return 1; if(n==2) return 2; return climbStairs(n-1) + climbStairs(n-2); } }; 非常不好意思,Tim

leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

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 Solution { public int climbSta

LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 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? 分析 动态规划基础题,首先设置3个变量用于

70. Climbing Stairs【leetcode】递归,动态规划,java,算法

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? Note: Given n will be a positive integer. 题目分析:每次只能走1或2步,问n步的话有多少中走法???? 可以用动态规划和递归解

leetcode 70 Climbing Stairs ----- java

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? 还是简单的动态规划问题,很明显可以发现. dp[i] = dp[i-2]+dp[i-1] public class Solution { public int clim