做了这道题目:
https://www.nowcoder.net/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=13&tqId=11161&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目本身比较简单,先用的递归来做的:
int jumpFloor2(int number) { if (number < 0) return 0; if (number < 2) return 1; return jumpFloor(number-1) + jumpFloor(number-2); }
耗时如下:
然后用的DP做的:
int jumpFloor2(int number) { if (number < 0) return 0; if (number < 2) return 1; int jump2 = 1; int jump1 = 1; for (int i=2; i<=number; i++) { int tmp = jump2 + jump1; jump2 = jump1; jump1 = tmp; } return jump1; }
耗时如下:
的确提高了很多倍啊!