[刷题]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?

思路:

1.当成数学题来思考,先确定最终结果由多少个1和多少个2来组成,然后把每种组成的排列组合累加起来。

 但是这样是很容易溢出的,13的阶乘就会使int溢出。

2.递归

 最后一步只可能是1或2。是1时,相当于在前一步的所有可能情况最后各加一个1,有f(n-1)中可能;是2时,相当于在前两步的所有可能情况最后各加一个2,有f(n-2)种可能。

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

然后,这不就是斐波拉切数列吗……你是不是想起了什么?把递归变为循环。

int climbStairs(int n) {
  if(n <= 2)
    return n;
  int last1 = 2;
  int last2 = 1;
  int ret;
  for(int i = 3; i<= n; i++){
    ret = last1 + last2;
       last2 = last1;
       last1 = ret;
   }
   return ret;
}
时间: 2025-01-09 06:59:31

[刷题]Climbing Stairs的相关文章

leetcode_70题——Climbing Stairs(简单DP题)

Climbing Stairs Total Accepted: 54579 Total Submissions: 158996My Submissions Question Solution 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

leetcode 刷题之路 92 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级,如果一次可以跳1级,也可以跳2级,求总共有多少总跳法. 分析: 一次最多只能跳两级,那么对于第n级(n>=2)台阶,显然只能从第n-1级跳一级到达或

刷题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 刷题总结

最近找工作,免不了看CC150 刷 LeetCode 来练手,练习之余也向各路大神 系统并且深入的学习.巩固一下算法知识.一. 线性表1. 数组    Remove Duplicates from Sorted Array    Remove Duplicates from Sorted Array II    Search in Rotated Sorted Array    Search in Rotated Sorted Array II    Median of Two Sorted A

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? 题解:这道题属于动态规划的题,类似于斐波那契数列,所以采用非递归的方式来解.当楼梯只有一级时,显然只有一种方法,即f(1

[LeetCode] Climbing Stairs (Sequence DP)

Climbing Stairs https://oj.leetcode.com/problems/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? 这题比较简单,可以使用动态规划来求解

[leetcode]Climbing Stairs @ Python

原题地址:https://oj.leetcode.com/problems/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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

54. Search a 2D Matrix &amp;&amp; Climbing Stairs (Easy)

Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer

[LeetCode] Climbing Stairs [24]

题目 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? 原题链接(点我) 解题思路 爬楼梯:一次可以爬1阶或者2阶,问爬n阶楼梯有多少方法? 这是个典型的斐波拉切应用场景,我们下面来分析下: 对于1阶,只有 1 种方法