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 distinct ways can you climb to the top?

题意:爬一层有n阶的楼梯,每次可以爬一阶或两阶,问爬到顶部有多少种方案

思路:dp,具体一点是斐波那契数列。f(i) = f(i-1) + f(i-2)

第i阶可以是从第i-2阶爬上来的,也可以是从第i-1阶爬上来的

进一步发现在迭代到第i阶时,我们只要保存前面的f(i-1)和f(i-2),

所以只要定义两个变量就可以,不用定义一个数组。

class Solution {
public:
    int climbStairs(int n){
    	int f1 = 1, f2 = 2, f3;
    	if(n == 1) return f1;
    	if(n == 2) return f2;
    	for(int i = 2; i < n; i++){
    		f3 = f1 + f2;
    		f1 = f2;
    		f2 = f3;
    	}
    	return f3;
    }
};

Leetcode 动态规划 Climbing Stairs

时间: 2024-11-10 13:38:03

Leetcode 动态规划 Climbing Stairs的相关文章

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 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 clim

【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 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][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 (爬楼梯) 解题思路和方法

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 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个变量用于

动态规划系列 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? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation: