题目:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
看题毫无头绪,不妨换一种思路,找找规律,
//台阶数 方法数
// 1 1
// 2 2
// 3 3
// 4 5
// 5 8
// 6 13
啊!这不就是那个斐波那契数列…
直接代码实现
public int climbStairs(int n) {
int count = 0;
int m = 0;
int a = 1;
int b = 2;
if (n<3){ m = n;}
if (n>=3){
while (true){
m=a+b;
a=b;
b=m;
count++;
if (count == (n-2)){
break;
}
}
}
return m;
}
看到一个老哥的猛男解法,给我看傻了?????????直接上代码分享给大家
int占用4字节,32比特,数据范围为-2147483648~2147483647[-2^31 ~
2^31-1]
Java的话因为返回值为int,n=46时,结果会溢出,因此n < 46,那么就有:
public int climbStairs(int n) {
int result = 0;
switch(n){
case 1: result = 1; break;
case 2: result = 2; break;
case 3: result = 3; break;
case 4: result = 5; break;
case 5: result = 8; break;
case 6: result = 13; break;
case 7: result = 21; break;
case 8: result = 34; break;
case 9: result = 55; break;
case 10: result = 89; break;
case 11: result = 144; break;
case 12: result = 233; break;
case 13: result = 377; break;
case 14: result = 610; break;
case 15: result = 987; break;
case 16: result = 1597; break;
case 17: result = 2584; break;
case 18: result = 4181; break;
case 19: result = 6765; break;
case 20: result = 10946; break;
case 21: result = 17711; break;
case 22: result = 28657; break;
case 23: result = 46368; break;
case 24: result = 75025; break;
case 25: result = 121393; break;
case 26: result = 196418; break;
case 27: result = 317811; break;
case 28: result = 514229; break;
case 29: result = 832040; break;
case 30: result = 1346269; break;
case 31: result = 2178309; break;
case 32: result = 3524578; break;
case 33: result = 5702887; break;
case 34: result = 9227465; break;
case 35: result = 14930352; break;
case 36: result = 24157817; break;
case 37: result = 39088169; break;
case 38: result = 63245986; break;
case 39: result = 102334155; break;
case 40: result = 165580141; break;
case 41: result = 267914296; break;
case 42: result = 433494437; break;
case 43: result = 701408733; break;
case 44: result = 1134903170; break;
case 45: result = 1836311903; break;
}
return result;
}
老哥原话:
长度较短的有限集合的解,可直接返回值,自己学习算法最终的目的还是为了更好地解决问题。
警醒自己不要沉迷于算法的精妙而忽视实际情况,上了很好的一课
令我感慨万千。