70. Climbing Stairs
2016-04-21 10:59
411 查看
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?
思路:
1 2 3 4 5 .....
1 2 3 5 8 .....
通过分析可以得到,其实就是一个斐波拉契数列
代码如下(已通过leetcode)
public class Solution {
public int climbStairs(int n) {
int[] a= new int
;
if(n==1) return 1;
a[0]=1;
a[1]=2;
int i;
for(i=2;i<n;i++) {
a[i]=a[i-1]+a[i-2];
}
return a[n-1];
}
}
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
思路:
1 2 3 4 5 .....
1 2 3 5 8 .....
通过分析可以得到,其实就是一个斐波拉契数列
代码如下(已通过leetcode)
public class Solution {
public int climbStairs(int n) {
int[] a= new int
;
if(n==1) return 1;
a[0]=1;
a[1]=2;
int i;
for(i=2;i<n;i++) {
a[i]=a[i-1]+a[i-2];
}
return a[n-1];
}
}
相关文章推荐
- CDISC SDTM PR domain 学习笔记
- Error:Cannot run program "svn" (in directory "E:\work\kuaisou_1.3.8\Hello"): CreateProcess error=2,
- 217. Contains Duplicate
- pthread_cond_wait()函数说明
- Saiku+Kylin多维分析平台探索
- LeetCode 172. Factorial Trailing Zeroes
- Domain Model(领域模型)
- System and Device Programming------Main Memory
- AIDL
- 人工智能60年:中国人工智能方阵发出第一轮AI冲击波
- 对Run-Time Check Failure #3 - The variable 'a' is being used without being initialized.的处理以及理解
- Paint和Canvas
- poj 2709 Painter
- 严重: WSSERVLET11: failed to parse runtime descriptor: The serviceName cannot be retrieved from an int
- 使用AirDrop分享
- 在其他class或者view中获取MainActivity实例,以便调用其函数的方法:
- zoj-3923- In the Rain
- hdu1867 A + B for you again(kmp)
- 【卡特兰数+高精度】HDU1023Train Problem II
- POJ 3735 Training little cats(矩阵快速幂)