File tree Expand file tree Collapse file tree 1 file changed +79
-0
lines changed Expand file tree Collapse file tree 1 file changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ # 跳台阶问题
2+
3+ ## 题目描述
4+
5+ 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
6+
7+ ## 跳台阶规律
8+
9+ ![ 20200701230719] ( http://cdn.heroxu.com/20200701230719.png )
10+
11+ ```
12+ 当 n = 1的时候, h = f(1) = 1
13+ 当 n = 2的时候, h = f(2) = 2
14+ 当 n = 3的时候, h = f(2) + f(1) = 2 + 1 = 3
15+ 当 n = 4的时候, h = f(3) + f(2) = 3 + 2 = 5
16+ ```
17+
18+ ## 实现-递归
19+
20+ ```
21+ public class Solution {
22+ public int JumpFloor(int target) {
23+ if(target==0){
24+ return 0;
25+ }else if(target==1){
26+ return 1;
27+ }else if(target==2){
28+ return 2;
29+ }else{
30+ int ret=0;
31+ int a=1;
32+ int b=2;
33+ for(int i=3;i<target+1;i++){
34+ ret=a+b;
35+ a=b;
36+ b=ret;
37+ }
38+ return ret;
39+ }
40+ }
41+ }
42+ ```
43+
44+ ---
45+
46+ ## 题目描述
47+
48+ 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
49+
50+ ## 找规律
51+
52+ ```
53+ 当 n = 1 1
54+ 当 n = 2 2
55+ 当 n = 3 4
56+ 当 n = 4 8
57+ 当 n = n 2^(n-1)
58+ ```
59+
60+ ## 实现-贪心
61+
62+ ```
63+ public class Solution {
64+ public int JumpFloorII(int target) {
65+ if(target==1){
66+ return 1;
67+ }else{
68+ int ret=0;
69+ int a=1;
70+ for(int i=2;i<target+1;i++){
71+ ret=2*a;
72+ a=ret;
73+ };
74+ return ret;
75+ }
76+
77+ }
78+ }
79+ ```
You can’t perform that action at this time.
0 commit comments