弟中弟的Leetcode总结——数组类(十)
弟中弟的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?
Note: Given n will be a positive integer.
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
思路
开始的时候想的是用回溯法进行递归,但是尝试提交了一次后发现超时了。因此想到了动态规划。
除了第一阶和第二阶楼梯以外,上到第n阶台阶的方法a(n)只有两种可能:从a(n-1)迈一步上去或者从a(n-2)迈两步上去。因此得到了状态转移方程a[i]=a[i-1]+a[i-2],且a[1]=1, a[2]=2.
代码(C)
int climbStairs(int n) {int ans[1000];ans[1]=1;ans[2]=2;for(int i=3;i<=n;i++){ans[i]=ans[i-1]+ans[i-2];}return ans[n];
}
弟中弟的Leetcode总结——数组类(十)相关推荐
- 弟中弟的Leetcode总结——数组类(十一)
弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...
- 弟中弟的Leetcode总结——数组类(五)
弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...
- 弟中弟的Leetcode总结——数组类(二)
弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...
- 弟中弟的Leetcode总结——数组类(四)
弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...
- 弟中弟的Leetcode总结——数组类(八)
弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...
- 弟中弟的Leetcode总结——数组类(九)
弟中弟的Leetcode总结--数组类(九) 题目描述 Subsets Given a set of distinct integers, nums, return all possible subs ...
- 弟中弟的Leetcode总结——数组类(六)
弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...
- 弟中弟的Leetcode总结——数组类(七)
弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...
- 弟中弟的Leetcode总结——字符串类(一)
弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...
最新文章
- 机器学习笔试题精选(二)
- FPGA设计思想之“逻辑复制”
- Google Map App 问题集锦
- jquery技巧(持续更新。。)
- github流程图_「强烈推荐」开源的在线流程图工具--draw.io
- VTK:vtkBoxWidget用法实战
- Linux命令执行顺序— ||和和; 比较
- iOS UITextField使用全攻略
- C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)
- Jenkins+Gitlab+ansible-playbook上线流程
- mysql linux 手动安装mysql服务_Linux手动安装Mysql
- mysql5.6 主从 延迟_MySQL5.6升级5.7时,出现主从延迟问题排查过程
- 把“~/niunantest.aspx”转成http://localhost:4532/test/niunantest.aspx的方法
- K8s宣布弃用Docker?千万别慌!
- 流模型 操作 xml
- 基于KINECT的拖拉机防撞系统
- 金仓数据库KingbaseES表空间(tablespace)知多少
- csdn竟然还有这种神器!后悔没有早点知道!超好用的csdn插件,别再犹豫了,赶快入手吧!
- 【LSTM+embeddingbag】进行文本分类完整代码~
- 第三章 Hello World入门程序