2023年1月12日 星期四

70. Climbing Stairs

解題心得

因為 n 的範圍不大,先算好全部,之後直接查。

程式碼

class Solution {
public:
    int climbStairs(int n) {
        int step[46] = {0, 1, 2};
        for(int i=3; i<=45; i++)
        {
            step[i] = step[i-1] + step[i-2];
        }
        return step[n];
    }
};

沒有留言:

張貼留言