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