내공얌냠 2022. 5. 31. 13:28

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

 

Climbing Stairs - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
public:
    int climbStairs(int n) {
        int a = 1, b = 1, temp = 0;
        for(int i = 2; i <= n; i++) {
            temp = a;
            a = a + b;
            b = temp;
        }
        return a;
    }
};

 

References

https://leetcode.com/problems/climbing-stairs/discuss/2087468/Easy-c%2B%2B-solution

https://leetcode.com/problems/climbing-stairs/discuss/2091389/100-Beginner-Friendly-Explanation

 

best

class Solution {
public:
    int climbStairs(int n) {
        int dp[n+1];
        dp[0]=1; dp[1]=1;
        for(int i=2;i<=n;i++){
            dp[i]=dp[i-1]+dp[i-2];
        }
        
        return dp[n];
    }
};
728x90
반응형