Climbing Stairs

ㅋㅋ·2022년 12월 12일
0

알고리즘-leetcode

목록 보기
67/135

계단을 오를 때 한 번에 1칸 혹은 2칸을 선택하여 올라갈 수 있다.

이 때 정수 n이 주어지고 n칸까지 올라 갈때 가능한 방법 수를 구하는 문제

class Solution {
public:
    int climbStairs(int n) {

        if (n == 1)
        {
            return 1;
        }
        
        int firstStep{1};
        int secondStep{2};

        int nextStep{};
        for (int i = 2; i < n; i++)
        {
            nextStep = firstStep + secondStep;
            firstStep = secondStep;
            secondStep = nextStep;
        }

        return secondStep;
    }
};

0개의 댓글