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?

  做这道题容易陷入的思维就是:所有step加起来的总和是n。俺这种思维去解决这个问题的话会显得很复杂。

  按动态规划的思维,要爬到第n阶,可以从第n-1阶爬1阶到达,也可以从第n-2阶爬2阶到达,因此爬到第n阶的方法有这么多种:

dp[n] = dp[n - 1] + dp[n - 2]

  按此思维的程序代码如下:

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         if(n == 0 || n == 1 || n == 2)
 5             return n;
 6
 7         int * dp = new int[n + 1];
 8         dp[0] = 0;
 9         dp[1] = 1;
10         dp[2] = 2;
11
12         for(int i = 3; i < n + 1; i++)
13             dp[i] = dp[i - 1] + dp[i - 2];
14
15         return dp[n];
16     }
17 };
时间: 2024-10-08 21:33:08

LeetCode之“动态规划”:Climbing Stairs的相关文章

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】#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] 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个台阶上

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阶楼梯,每次只能爬1阶或2阶楼梯,问爬到第n阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

【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】070. 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. 题解: 爬到第n层的方法要么是从第n-1层一步上来的,要不就是从n-2层2步

leetcode || 70、 Climbing Stairs

problem: 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 题意:爬上一个 n阶 的楼梯,每次爬一阶或者两阶,求一共有多少不同的方法 thinking

[LeetCode] Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step w

LeetCode题解之Climbing Stairs

1.题目描述 2.问题分析 使用动态规划. 3.代码 1 int climbStairs(int n) { 2 if( n <= 2){ 3 return n; 4 } 5 6 int dp[n+1]; 7 dp[0] = 0; 8 dp[1] = 1; 9 dp[2] = 2; 10 for( int i = 3; i <= n ; i++){ 11 dp[i] = dp[i-1] + dp[i-2]; 12 } 13 14 return dp[n]; 15 } 原文地址:https://w

[LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step w