2022 9/30 12:36
路漫漫其修远兮,吾将上下而求索
本文是根据尚硅谷学习所做笔记
仅供学习交流使用,转载注明出处
【尚硅谷经典Java面试题第一季(java面试精讲)-哔哩哔哩】
编程题:
有 n 步台阶,一次只能上 1 步或者 2 步,共有多少种走法?
1.递归
2.循环迭代
斐波那契数列

package fbnq5;
import org.junit.Test;
public class TestStep {
@Test
public void test() {
long start=System.currentTimeMillis();
System.out.println(f(40));//165580141
long end=System.currentTimeMillis();
System.out.println(end-start);//335
}
//实现f(n):求n步台阶,一个有几种走法
public int f(int n){
if (n<1){
throw new IllegalArgumentException(n+"不能小于1");
}
if (n==1||n==2){
return n;
}
return f(n-2)+f(n-1);
}
}

package fbnq5;
import org.junit.Test;
public class TestStep {
@Test
public void test() {
long start=System.currentTimeMillis();
System.out.println(f(40));//165580141
long end=System.currentTimeMillis();
System.out.println(end-start);//335
}
//实现f(n):求n步台阶,一个有几种走法
public int f(int n){
if (n<1){
throw new IllegalArgumentException(n+"不能小于1");
}
if (n==1||n==2){
return n;
}
return f(n-2)+f(n-1);
}
}
方法调用自身称为递归,利用变量的原值推出新值称为迭代。
递归
迭代
2022 9/30 13:11
p5
Markdown 1251 字数 121 行数 当
HTML 1053 字数 69 段落