70 climbing stairs

·data-structure-and-algorithm
#dynamic-programming

70. 爬楼梯

go:

空间优化:

func climbStairs(n int) int {
    if n < 2 {
        return n
    }

    a, b := 1, 2
    for i := 3; i <= n; i++ {
        a, b = b, a+ b
    }

    return b
}

常规流程:

func fib(n int) int {
    if n < 2 {
        return n
    }
    // dp[i] 就代表 F(i)
    dp := make([]int, n+1)
    dp[0], dp[1] = 1, 2
    for i := 3; i <= n; i++ {
        dp[i] = dp[i-1] + dp[i-2]  // 状态转移方程
    }
    return dp[n]
}```