LEETCODE: Climbing Stairs
2014-12-22 16:49
281 查看
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?
如果样本数量更大一些,这个算法可能还是不能承受。
class Solution {
public:
int cnk(int n, int k) {
long long up = 1;
long long down = 1;
for(int ii = 0; ii < k; ii ++) {
down *= n - ii;
up *= k - ii;
// Maybe large than LONG_MAX.
if(down % 2 == 0 && up % 2 == 0) {
down /= 2;
up /= 2;
}
}
return down / up;
}
int climbStairs(int n) {
int maxnum2 = n / 2;
int result = 0;
for(int ii = 0; ii <= maxnum2; ii ++) {
result += cnk(n - ii, ii);
}
return result;
}
};
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
如果样本数量更大一些,这个算法可能还是不能承受。
class Solution {
public:
int cnk(int n, int k) {
long long up = 1;
long long down = 1;
for(int ii = 0; ii < k; ii ++) {
down *= n - ii;
up *= k - ii;
// Maybe large than LONG_MAX.
if(down % 2 == 0 && up % 2 == 0) {
down /= 2;
up /= 2;
}
}
return down / up;
}
int climbStairs(int n) {
int maxnum2 = n / 2;
int result = 0;
for(int ii = 0; ii <= maxnum2; ii ++) {
result += cnk(n - ii, ii);
}
return result;
}
};
相关文章推荐
- [leetcode]Climbing Stairs
- LeetCode:Climbing Stairs
- leetcode - Climbing Stairs
- [Leetcode]Climbing Stairs
- LeetCode-70-Climbing Stairs(动态规划)-Easy
- LeetCode 70 Climbing Stairs
- LeetCode_70---Climbing Stairs
- LeetCode | Climbing Stairs
- 《leetCode》:Climbing Stairs
- Leetcode: Climbing stairs
- 【leetcode】Climbing Stairs
- leetcode 107: Climbing Stairs
- Leetcode:Climbing Stairs
- LeetCode_Climbing Stairs
- [LeetCode] Climbing Stairs (DP)
- Leetcode: Climbing Stairs
- LeetCode——Climbing Stairs
- leetcode-climbing stairs
- [leetcode] #70 Climbing Stairs
- ***(leetcode) Climbing Stairs