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层的方式(爬两层)的和

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. if(n <= ) return ;
  5. else if(n == ) return ;
  6. else if(n == ) return ;
  7. else
  8. {
  9. int way1 = ;
  10. int way2 = ;
  11. int ans = ;
  12. for(int i = ; i <= n; i++)
  13. {
  14. ans = way1 + way2;
  15. way2 = way1;
  16. way1 = ans;
  17. }
  18. return ans;
  19. }
  20. }
  21. };

【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. mysql搜索引擎 小结

    mysql搜索引擎 小结 mysql5.5以后,mysql默认使用InnoDB存储引擎. 若要修改默认引擎,可以修改配置文件中的default-storage-engine.可以通过show vari ...

  2. Redis安装和使用

    新年伊始,万象更新.祝大家:新的一年,工作顺利,生活越来越美好. http://www.redis.cn/commands.html http://try.redis.io/ http://www.c ...

  3. PHP简单封装MysqlHelper类

    MysqlHelper.class.php 1: <?php 2:  3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: func ...

  4. Shanghai Regional Online Contest 1004

    Shanghai Regional Online Contest 1004 In the ACM International Collegiate Programming Contest, each ...

  5. MSP430G2333下位机乘法运算需要注意的一个问题

    背景: 最近负责为主板管理电源的电源管理模块编写软体,使用的MCU为MSP430G2333.功能上很简单,即通过板子上的硬件拨码设定,或者通过IIC与主板通信,由主板的BIOS决定开机及关机的延时供电 ...

  6. Jquery简单瀑布流代码示例

    最近很多网站都采用瀑布流风格设计,感觉挺有个性的,比较合适做图片类型的网站,没事仿开心网做一个瀑布流示例. 需要用到Jquery,jquery.masonry.min.js <!DOCTYPE ...

  7. JQuery实战手风琴-遁地龙卷风

    (-1)写在前面 这个图片是我从网上下载的,向这位前辈致敬.图片资源在我的百度云盘里.http://pan.baidu.com/s/1nvfJHdZ 我用的是chrome49,JQuery3.0,案例 ...

  8. vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面

    vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...

  9. XML类似的解析时,会遇到'XXX' 不是 'NCName' 的有效值的问题

    主要原因是:xml中或类xml的文件中有些关键属性的值不符合NCName命名规范,例如我遇到的是流程的bpmn文件中,id的属性值命名的数字开头. NCName 不包含冒号 (:) 的 XML 名称. ...

  10. iOS开发——UI进阶篇(一)UITableView,索引条,汽车数据展示案例

    一.什么是UITableView 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableViewUITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳 UIT ...