leecode 解题总结:70. Climbing Stairs
来源:互联网 发布:flac播放软件 编辑:程序博客网 时间:2024/06/15 10:59
#include <iostream>#include <stdio.h>#include <vector>using namespace std;/*问题: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个台阶的楼梯,每次只允许爬一个或者两个,问总共有多少种方式。这个是动态规划设dp[i]表示到达第i层台阶的方式,则dp[i] = dp[i-1] + dp[i-2]边界:dp[1] = 1,dp[2]=2目标:dp[n]dp[3]1,1,12,11,2所以dp[3]共有3种输入:123输出:123*/class Solution {public: int climbStairs(int n) {if(n <= 0){return 0;}if(n <= 2){return n;} vector<int> steps(n + 1 , 0);//初始化边界值steps.at(1) = 1;steps.at(2) = 2;for(int i = 3 ; i <= n ; i++){steps.at(i) = steps.at(i-1) + steps.at(i-2);}return steps.at(n); }};void process(){ int num; Solution solution; while(cin >> num ) { int result = solution.climbStairs(num); cout << result << endl; }}int main(int argc , char* argv[]){process();getchar();return 0;}
0 0
- leecode 解题总结:70. Climbing Stairs
- [leetcode] 70. Climbing Stairs 解题报告
- LeetCode解题报告 70. Climbing Stairs [easy]
- [Leetcode] 70. Climbing Stairs 解题报告
- Leetcode Climbing Stairs 解题报告
- 【LeetCode】Climbing Stairs 解题报告
- Leetcode 70. Climbing Stairs 爬楼梯 解题报告
- Leetcode解题笔记 70. Climbing Stairs [Easy] 动态规划
- [LeetCode]70.Climbing Stairs
- 70.Climbing Stairs
- LeetCode --- 70. Climbing Stairs
- [Leetcode] 70. Climbing Stairs
- [leetcode] 70.Climbing Stairs
- 70.Climbing Stairs
- 70. Climbing Stairs
- 70.Climbing Stairs
- 70. Climbing Stairs
- [leetCode]70. Climbing Stairs
- 最短路+差分约束学习笔记
- css布局总结
- JZOJ 4061. 【JSOI2015】字符串树
- Android 的简单应用(MusicPlayer) 以及 Soundpool池
- Linux 线程简析
- leecode 解题总结:70. Climbing Stairs
- THUWC2017 酱油记
- boost处理日期和时间的总结
- 【JSOI2015】送礼物
- Servlet3.0包依赖问题
- 从栈和堆中来看值传递和引用传递
- Linux 常用环境变量及作用和环境变量文件的详细介绍及其加载执行顺序
- PAT甲级真题1012. The Best Rank (25)
- 自然语言处理与深度学习: 集智俱乐部活动笔记