2的n-1次幂求解
变态跳台阶
http://www.nowcoder.com/questionTerminal/22243d016f6b47f2a6928b4313c85387
本题的思路:
台阶数 --> 跳跃次数
0 --> 1
1 --> 1
2 --> 2 = 2^1
3 --> 4 = 2^2
4 --> 8 = 2^3
...
n --> 2^(n-1)
分析可知n阶台阶的跳跃方法为2^(n-1),c++代码如下:
class Solution {
public:
int jumpFloorII(int number) {
if((0 == number) || (1 == number)) {
return 1;
}
int sum = 1;
for(int i=2; i<=number; i++) {
sum *= 2;
}
return sum;
}
};