70. Climbing Stairs

    由于每次只能迈1步或者两步台阶

    拆解:

    1. 起始状态
      1. nums[0] = 1、nums[1] = 1
    2. 状态转移方程
      1. nums[i] = nums[i-1] + nums[i-2]

    当然还可以优化:

    1. public int climbStairs(int n) {
    2. if(n == 0 || n == 1) {
    3. return 1;
    4. }
    5. int prev = 1;
    6. int cur = 1;
    7. for (int i = 2; i <= n; i++) {
    8. int tmp = cur;
    9. cur = cur + prev;
    10. prev = tmp;
    11. }
    12. return cur;
    13. }