【leetcode】Climbing Stairs (easy)
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层的方式(爬一层)和到第n-2层的方式(爬两层)的和
class Solution {
public:
int climbStairs(int n) {
if(n <= ) return ;
else if(n == ) return ;
else if(n == ) return ;
else
{
int way1 = ;
int way2 = ;
int ans = ;
for(int i = ; i <= n; i++)
{
ans = way1 + way2;
way2 = way1;
way1 = ans;
}
return ans;
}
}
};
【leetcode】Climbing Stairs (easy)的更多相关文章
- 【leetcode】Climbing Stairs
题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- 【题解】【DP】【Leetcode】Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Plus One (easy)
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- WCF绑定类型选择
WCF绑定类型选择 发布日期:2010年12月10日星期五 作者:EricHu 在开发WCF程序时,如何选择一个适合的绑定对于消息传输的可靠性,传输模式是否跨进程.主机.网络,传输模式的支持. ...
- 【AngularJS】—— 11 指令的交互
前面基本了解了指令的相关内容: 1 如何自定义指令 2 指令的复用 本篇看一下指令之间如何交互.学习内容来自<慕课网 指令3> 背景介绍 这例子是视频中的例子,有一个动感超人,有三种能力, ...
- AngularJS 国际化——Angular-translate
对于一个用户群面向全球的的应用来说,不得不考虑国际化的问题.当然,即便是刚刚起步的小应用,如果有心搞大,也应该提前设计国际化的方案. 本篇讲述使用AngularJS构建的应用的简单国际化方案,准确的说 ...
- 5、数组和集合--Collection、Map
一.数组:同一个类型数据的集合,其实他也是一个容器 1.数组的好处:可以自动给数组中的元素从0开始编号,方便操作这些数据 2.数组的定义: 在Java中常见: 格式1: 类型 [] 数组名 = ne ...
- ajax基础了解
使用Ajax的最大优点,就是能在不更新整个页面的前提下维护数据.这使得Web应用程序更为迅捷地回应用户动作,并避免了在网络上发送那些没有改变过的信息.AJAX即“Asynchronous JavaSc ...
- 新年新技术:MongoDB 3.0
前一篇介绍了HTTP/2,这一篇简单介绍下3月3号发布的MongoDB 3.0. What’s new in MongoDB 3.0? 新的存储引擎WiredTiger MongoDB 3.0的存储引 ...
- 真机调试 —— An unknown error occurred.
iOS开发中,总会遇见各种各样的问题.今天我就在真机调试的时候出现 An unknown error occurred. 不知道什么鬼,百度一下,各种胡说八道. 解决办法: 1.退出Xcode,重新运 ...
- 【PHP面向对象(OOP)编程入门教程】21.多态的应用
多态是除封装和继承之外的另一个面象对象的三大特性之一,我个人看来PHP中虽然可以实现多态,但和c++还有Java这些面向对象的语言相比,多 态性并不是那么突出,因为PHP本身就是一种弱类型的语言,不存 ...
- C#调用java类、jar包方法
一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . 其中tes ...
- (备忘)android模拟器摄像头模拟
Camera分Front Camera和Back Camera 通常我们模拟后摄像头就可以了 三个选项 none:表示没有摄像头,打开摄像应用会崩溃 emulated:系统模拟一个动态的画面--在黑白 ...