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)的更多相关文章

  1. 【leetcode】Climbing Stairs

    题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  2. 【题解】【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 ...

  3. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. 【leetcode】Plus One (easy)

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  7. 【leetcode】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  8. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. 【leetcode】Majority Element (easy)(*^__^*)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. JS keycode 事件响应

    <script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...

  2. ASP跨域调用Webservices方法

    仅用于记录与分享,直接贴代码: <script type="text/javascript"> function check(){ var title=$('#titl ...

  3. UI第三节—— UITextField详解

    戏言:UITextField对于需要登陆注册的界面的作用还是相当明显,但是对于键盘过的遮挡问题,可是重点哦!这里就涉及到通知(NSNotificationCenter)的内容. //注册事件 [[NS ...

  4. python下载网页源码 写入文本

    import urllib.request,io,os,sysreq=urllib.request.Request("http://echophp.sinaapp.com/uncategor ...

  5. json不转化值是null的字段

    今天写东西,发现JSONObject.fromObject(),方法,会把value是null的字段,转为0或"",就自己写了一个方法,如果value是null就不转换 packa ...

  6. 架设WEBIM

    使用openfire+jwchat构建. Openfire 采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.Openfire安装和使用都非常简单,并利用Web进行管理 ...

  7. UIMenuController使用

    - (void)bubbleDidLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { if(gestureRecognizer. ...

  8. $.extend()了解心得

    2.1 extend(result,item1,item2-..) 这里这个方法主要用来合并,将所有的参数项都合并result中,并返回result,但是这 样就会破坏result的结构. 2.2 e ...

  9. 获取客户端IP

    function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...

  10. rest api设计的一般原则

    本文参考自:http://www.ruanyifeng.com/blog/2014/05/restful_api.html,http://www.dongming8.cn/?p=590 服务器端: 1 ...