- 爬樓梯
MIOJ 爬樓梯
package mioj.day4.ClimbStairs;
import java.util.Scanner;
/**
* @author: chuanyi@88.com
* @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;
}
/**
* 動態(tài)規(guī)劃法
* @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]