- 爬楼梯
MIOJ 爬楼梯
package mioj.day4.ClimbStairs;
import java.util.Scanner;
/**
* @author: [email protected]
* @description: mioj: 爬楼梯 https://code.mi.com/problem/list/view?id=10&cid=14
* @date: 2020/10/24 20:27
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int num = scanner.nextInt();
long start = System.currentTimeMillis();
System.out.println("1.climbStairsRecursive result: " + climbStairsRecursive(num));
long time1 = System.currentTimeMillis();
System.out.println("1.climbStairsRecursive time cost:" + (time1 - start) + " ms");
System.out.println("2.climbStairsRecursiveImproved result: " + climbStairsRecursiveImproved(num));
long time2 = System.currentTimeMillis();
System.out.println("2.climbStairsRecursiveImproved time cost:" + (time2 - time1) + " ms");
System.out.println("3.climbStairsIterative result: " + climbStairsIterative(num));
long time3 = System.currentTimeMillis();
System.out.println("3.climbStairsIterative time cost:" + (time3 - time2) + " ms");
System.out.println("4.climbStairsDp result: " + climbStairsDp(num));
long time4 = System.currentTimeMillis();
System.out.println("4.climbStairsDp time cost:" + (time4 - time3) + " ms");
System.out.println("5.climbStairsSlippedWindow result: " + climbStairsSlippedWindow(num));
long time5 = System.currentTimeMillis();
System.out.println("5.climbStairsSlippedWindow time cost:" + (time5 - time4) + " ms");
}
}
/**
* 递归做法
*
* @param num
* @return
*/
private static int climbStairsRecursive(int num) {
if (num < 1) {
return 0;
}
if (num <= 2) {
return num;
}
return climbStairsRecursive(num - 1)
+ climbStairsRecursive(num - 2);
}
/**
* 递归做法存储改进版.有人叫它备忘录法
*
* @param num
* @return
*/
private static int climbStairsRecursiveImproved(int num) {
if (num < 1) {
return 0;
}
if (num <=2){
return num;
}
int[] count = new int[num+1];//注意第一下标丢弃不用
return climbStairsRecursive(num - 1, count)
+ climbStairsRecursive(num - 2, count);
}
private static int climbStairsRecursive(int num, int[] count) {
if (num <= 2) {
count[num] = num;
} else {
count[num] = climbStairsRecursive(num - 1, count)
+ climbStairsRecursive(num - 2, count);
}
return count[num];
}
/**
* 迭代
*
* @param num
* @return
*/
private static int climbStairsIterative(int num) {
if (num < 1) {
return 0;
}
int cur = 1;
int pre = 0;
for (int i = 0; i < num; i++) {
int temp = cur;
cur += pre;
pre = temp;
}
return cur;
}
/**
* 动态规划法
* @param num
* @return
*/
private static int climbStairsDp(int num){
if (num < 1){
return 0;
}
if (num <= 2){
return num;
}
int[] count = new int[num+1];//这里下标从1开始使用,比较容易理解和表示
count[1] = 1;
count[2] = 2;
for (int i = 3; i <= num; i++) {
count[i] = count[i -1] + count[i - 2];
}
return count[num];
}
/**
* 每个结果只和上一个和上上一个结果有关,所以每次只保留这两个结果即可
* @param num
* @return
* 3
* count 1 2 3
* cur 1 1 2 2
* pre 1 1 1
* prepre 0 1 1
* temp 1 1 2
* temp2 1 1 1
*
*
*
*/
private static int climbStairsSlippedWindow(int num){
if (num < 1){
return 0;
}
if (num <= 2){
return num;
}
int count = 0;
int cur = 0;
int pre = 1;
int prepre = 0;
while (count < num){
cur = pre + prepre;
prepre = pre;
pre = cur;
count ++;
}
return cur;
}
}
参考: (爬楼梯问题)[https://blog.csdn.net/wu2304211/article/details/52717578?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param]