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://www.cnblogs.com/wangxiaoyong/p/9575339.html

时间: 2024-10-04 12:36:01

LeetCode题解之Climbing Stairs的相关文章

[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】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笔记: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

一天一道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】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 || 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] 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

Climbing Stairs leetcode java

题目: 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问题的问题.. 假设现在在已经爬上来了,你之前那一步是怎么爬的,只有两种选择,1步上来的,或者2步上来的.顺藤摸瓜.写好