爬楼梯的问题Climbing Stairs
2016-04-21 17:36
357 查看
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?
Subscribe to see which companies asked this question
==============================================================================
//类似于斐波那契数列
public class Solution {
public int climbStairs(int n) {
if(n <= 0) return 0;
if(n == 1) return 1;
if(n == 2) return 2;
int one_step_before = 2;
int two_steps_before = 1;
int all_ways = 0;
for(int i=2; i<n; i++){
all_ways = one_step_before + two_steps_before;
two_steps_before = one_step_before;
one_step_before = all_ways;
}
return all_ways;
}
}
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Subscribe to see which companies asked this question
==============================================================================
//类似于斐波那契数列
public class Solution {
public int climbStairs(int n) {
if(n <= 0) return 0;
if(n == 1) return 1;
if(n == 2) return 2;
int one_step_before = 2;
int two_steps_before = 1;
int all_ways = 0;
for(int i=2; i<n; i++){
all_ways = one_step_before + two_steps_before;
two_steps_before = one_step_before;
one_step_before = all_ways;
}
return all_ways;
}
}
相关文章推荐
- 十、责任链模式Chain of Responsibility(行为型模式)
- http://blog.csdn.net/holybin/article/details/46955185
- 服务器端主动关闭连接, 产生的TIME_WAIT状态为什么会占用服务端大量端口?
- HoloLens开发手记-硬件细节 Hardware Detail
- git修改提交的用户名和Email
- 03-树3 Tree Traversals Again
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 正确使用Block避免Cycle Retain和Crash
- lightoj 1076 - Get the Containers 【二分 + 贪心】
- Failed to read artifact descriptor进行安装maven项目出错
- AI编辑SVG格式的相关问题
- paip.自适应网页设计 跟 响应式 设计的区别跟原理and实践总结
- 图解RAID 0, RAID 1, RAID 5, RAID 10
- go gomail
- R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)
- kaidi中 install cuda
- stl map用法和make_pair函数
- 关于解决 Failed to prepare partial IU:
- HDU 1848 Fibonacci again and again SG函数详解!
- socket服务器开发中的SO_REUSEADDR选项与让人心烦的TIME_WAIT