LeetCode70 Climbing Stairs
2017-08-06 13:37
369 查看
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?
Note: Given n will be a positive integer.
解答:
public class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2) return n;
int[] m=new int
;
m[0]=1;
m[1]=2;
for(int i=2;i<n;i++){
m[i]=m[i-1]+m[i-2];
}
return m[n-1];
}
}
一次一步或一次两步,那么第M阶可能是由M-1或M-2爬上来的。
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
解答:
public class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2) return n;
int[] m=new int
;
m[0]=1;
m[1]=2;
for(int i=2;i<n;i++){
m[i]=m[i-1]+m[i-2];
}
return m[n-1];
}
}
一次一步或一次两步,那么第M阶可能是由M-1或M-2爬上来的。
相关文章推荐
- LeetCode 70 Climbing Stairs
- LeetCode70——Climbing Stairs
- [leetcode 70]Climbing Stairs
- LeetCode【70】Climbing Stairs
- leetcode70-Climbing Stairs
- LeetCode 70 Climbing Stairs
- Leetcode-70 Climbing Stairs
- 【Leetcode-Easy-70】Climbing Stairs
- Climbing Stairs | leetcode 70 【Java解题报告】
- LeetCode - 70 - Climbing Stairs
- leetcode——70——Climbing Stairs
- Leetcode 70 Climbing Stairs
- LeetCode 70 Climbing Stairs--Python实现
- leetcode 70 Climbing Stairs
- 【LeetCode】C# 70、Climbing Stairs
- LeetCode_70---Climbing Stairs
- LeetCode 70 — Climbing Stairs(C++ Java Python)
- LeetCode 70:Climbing Stairs
- Leetcode_70_Climbing Stairs
- LeetCode-70-Climbing Stairs-E