本题来自《剑指offer》 跳台阶

题目1:

  一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

思路:

  同上一篇。

C++ Code:

class Solution {
public:
int jumpFloor(int number) {
int one = ;
int two = ;
int floor = ;
for (unsigned int i =;i<=number;i++){
floor = one + two;
one = two;
two = floor;
}
return floor;
}
};

Python Code:

# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
one = 1
two = 1
for i in range(number):
one,two = two,one+two
return one

题目2:变态跳台阶

  一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

思路:

  归纳总结共有2的n-1次方种跳法,所以直接返回数学表达式。

Python code:

# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
floor = 1
for i in range(number-1):
floor *= 2
return floor

最新文章

  1. Mac上搭建Nginx + rtmp
  2. PHP定界符使用技巧
  3. Scrum Meeting 11-20151217
  4. VC++修改电脑系统时间
  5. 解决64位Windows2003程序字体很小的问题
  6. 第 19 章 CSS 其他样式
  7. System.TypeInitializationException: The type initializer for &#39;Mono.Unix.Native.Stdlib&#39; threw an exception.
  8. jQuery 停止动画
  9. matplotlib绘制精美的图表(这是教程哦)
  10. Min and Max
  11. redis简单配置
  12. Winform DataGridView CheckBoxColumn c# 单选 解决方案
  13. 判断浏览器是否支持html5和css3属性
  14. [NOI 2010]超级钢琴
  15. Linux上rpm实战搭建FTP服务器
  16. OO第二次博客作业—17373247
  17. [python,2019-02-15] 最短回文串
  18. 《转载》python爬虫实践之模拟登录
  19. 【win10】更改资源管理器显示:快速访问和此电脑
  20. Java 代码性能调优“三十六”策

热门文章

  1. constraintLayout的一些高级用法 布局一个16:9的图片 以及GuideLine的使用
  2. Light oj 1018 - Brush (IV) 状态压缩
  3. 20165237 2017-2018-2 《Java程序设计》第四周考试补做及2-3章编程题
  4. Aurora 安装
  5. 【转】python 退出程序的方式
  6. 【转】Git超实用总结,再也不怕记忆力不好了
  7. 给你的Linux系统上点stress【转】
  8. 专题2:最长上升子序列LIS
  9. Unity3D游戏开发框架-资源管理类ResourceManage
  10. redis递减,过期返回值