Leetcode#70Climbing 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(n)=f(n-1)+f(m-2),然而时间复杂度太大,因此类似问题可以采用动态规划的思想,如下代码

public class Solution {

public int climbStairs(int n) {

if(n<=2)

return n;

else

{

int[] kinds=new int[n];

kinds[0]=1;

kinds[1]=2;

for(int i=2;i<n;i++)

{

kinds[i]=kinds[i-1]+kinds[i-2];

}

return kinds[n-1];

}

}

}

时间: 2024-08-11 01:35:44

Leetcode#70Climbing Stairs的相关文章

[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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

[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 种方法

LeetCode: Climbing Stairs [070]

[题目] 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阶,为爬到梯子顶共有多少种爬法 [思路] 依次确定跳到每一阶上的爬法数目 这其实是一个斐波那契数列数列.假设A[

[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

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? //第一种解法 class Solution { public: int climbStairs(int n) { int ans[100] = {1,2}; for

[leetcode]_Climbing Stairs

我敢保证这道题是在今早蹲厕所的时候突然冒出的解法.第一次接触DP题,我好伟大啊啊啊~ 题目:一个N阶的梯子,一次能够走1步或者2步,问有多少种走法. 解法:原始DP问题. 思路: 1.if N == 1 , then ans = 1; 2.if N == 2 , then ans = 2; 3.if 我们现在在N-1阶处,现在已经有f(N-1)种走法,那么到N阶处,则还是f(N - 1)种走法(再走一步到终点). 4.if 我们现在在N-2阶处,现在已经有f(N-2)种走法,那么到N阶处,则还是

Leetcode: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层有f(n)中走法,因为每次只能走1步或2步,则 f(n) = f(n-1) + f(n-2) 很明显是一个斐波那契数,f(0) = 0,

[LeetCode] 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? Show Tags 这题其实就是斐波那契数列来的. #include <iostream> using namespace std; class Solution {

LeetCode Climbing Stairs 爬楼梯

递归法(TLE代码): 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==0) 5 return 1; 6 if(n<0) 7 return 0; 8 return (climbStairs(n-1)+climbStairs(n-2)); 9 } 10 }; 动态规划法: 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==1) return