70. 爬楼梯

70. 爬楼梯

https://leetcode-cn.com/problems/climbing-stairs/description/

package com.test;

import java.util.HashMap;
import java.util.Map;

public class Lesson070 {
    public static void main(String[] args) {
        int n = 45;
        int i = climbStairs(n);
        System.out.println(i);
    }

    public static int climbStairs(int n) {
        Map<Integer,Integer> map= new HashMap<>();
        map.put(1, 1);
        map.put(2, 2);
        map.put(3		,3);
        map.put(4		,5);
        map.put(5		,8);
        map.put(6		,13);
        map.put(7		,21);
        map.put(8		,34);
        map.put(9		,55);
        map.put(10		,89);
        map.put(11		,144);
        map.put(12		,233);
        map.put(13		,377);
        map.put(14		,610);
        map.put(15		,987);
        map.put(16		,1597);
        map.put(17		,2584);
        map.put(18		,4181);
        map.put(19		,6765);
        map.put(20		,10946);
        map.put(21		,17711);
        map.put(22		,28657);
        map.put(23		,46368);
        map.put(24		,75025);
        map.put(25		,121393);
        map.put(26		,196418);
        map.put(27		,317811);
        map.put(28		,514229);
        map.put(29		,832040);
        map.put(30		,1346269);
        map.put(31		,2178309);
        map.put(32		,3524578);
        map.put(33		,5702887);
        map.put(34		,9227465);
        map.put(35		,14930352);
        map.put(36		,24157817);
        map.put(37		,39088169);
        map.put(38		,63245986);
        map.put(39		,102334155);
        map.put(40		,165580141);
        map.put(41		,267914296);
        map.put(42		,433494437);
        map.put(43		,701408733);
        map.put(44		,1134903170);
        map.put(45		,1836311903);
        for(int i=3;i<Integer.MAX_VALUE;i++) {
            Integer i1 = map.get(i - 1);
            Integer i2 = map.get(i - 2);
            if (i1 + i2 < 0) {
                break;
            }
            map.put(i,i1+i2);
            System.out.println("map.put("+i+"		,"+map.get(i)+");");
        }
        return map.get(n);
        // 每次都有两种选择
//        if (n - 1 == 0) {
//            return 1;
//        }
//        if (n - 2 == 0) {
//            return 2;
//        }
//        if (n - 44 == 0) {
//            return 1134903170;
//        }
//        // 向后回溯只是两种情况的累加
//        return climbStairs(n-1)+climbStairs(n-2);
    }
}
原文地址:https://www.cnblogs.com/stono/p/9533117.html