[LeetCode OJ]-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=3时有3种方法,有几个叶子节点便有几种方法

 1 void climb( int remainder, int &way)
 2 {
 3     if(remainder==0 || remainder==1)
 4     {
 5         way++;
 6         return;
 7     }
 8
 9     climb(remainder-1, way);
10     climb(remainder-2, way);
11     return;
12 }
13
14 class Solution {
15 public:
16     int climbStairs(int n) {
17         int result=0;
18         climb(n, result);
19         return result;
20     }
21 };

但是这种方法对于n比较大时会超时,在测试用例中对于n=38就会TLE(Time Limit Exceed)。

方法二:

总结规律,通过以下数据,我们发现

way(1)=1

way(n) = way(n-1) + Fibonacci(n-1)    n=2,3,4,5,6,....

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         int result=1;
 5         int Fibonacci_0=0, Fibonacci_1=1, temp;
 6         for(int i=1; i<n; i++)
 7         {
 8             result += Fibonacci_1;
 9             temp = Fibonacci_1;
10             Fibonacci_1 = Fibonacci_0 + Fibonacci_1;
11             Fibonacci_0 = temp;
12
13         }
14         return result;
15     }
16 };

对于leetcode中所有的测试数据都可以通过

[LeetCode OJ]-Climbing Stairs,布布扣,bubuko.com

时间: 2024-12-25 22:45:13

[LeetCode OJ]-Climbing Stairs的相关文章

【LeetCode】Climbing Stairs

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

Leetcode 动态规划 Climbing Stairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Climbing Stairs Total Accepted: 13319 Total Submissions: 40778 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 distinc

LeetCode | 0070. Climbing Stairs爬楼梯【Python】

LeetCode 0070. Climbing Stairs爬楼梯[Easy][Python][动态规划] Problem LeetCode 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

[LeetCode][JavaScript]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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

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)

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】Climbing Stairs (2 solutions)

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? 这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2) 因此建立vector存放中间结果即可. class Solution

[leetcode] 14. Climbing Stairs

这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: 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 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个变量用于