LintCode Climbing Stairs 爬楼梯
2015-07-11 22:05
627 查看
中文描述:
假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?
样例
比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法
返回 3
English Version:
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?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3
假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?
样例
比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法
返回 3
English Version:
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?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3
public class Solution { /** * @param n: An integer * @return: An integer */ //法一:动归 public int climbStairs(int n) { int one = 0; int two = 1; while(n>0) { two=one+two; one=two-one; n--; } return two; } //法二:递归迭代,超时 public int climbStairs(int n) { if(n==1)return 1; if(n==2)return 2; return climbStairs(n-1)+climbStairs(n-2); } }
相关文章推荐
- 【概率】Uva 10900 - So you want to be a 2n-aire?
- EF--Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
- Svnmailer部署及应用
- AIX系统维护手册二.双机配置,HACMP
- AIX系统维护手册一.系统盘镜像
- my email is nuoyaxingzhe@sina.com
- @(报错)could not find the main class, Program will exit(已解决)
- 2965 - Jurassic Remains
- AIX查看某个端口被哪个进程占用并处理
- LeetCode题解——Container With Most Water
- Sicily 1509. Rails
- CLOSE_WAIT状态的原因与解决方法
- Altair.HWDesktop.v13.0.112.HotFix.Win64.&.Linux64 2CD有限元分析
- An error occurred trying to connect: Post https://192.168.59.103:2376/v1.19/containers/create
- Leetcode算法刷题:217和219题 Contains Duplicate
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 篮球游戏AI预研
- 现代人工智能走在仿生学的大道上
- rails开发demo(一)搭建环境
- Ecmascript-262-3 in detail. Chapter 4. Scope chain.