Algorithm/개념 정리
Climbing Stairs를 recursive 아닌 방법으로 풀어보기
JunGi Jeong
2021. 4. 13. 11:34
전에 Recursive로 풀었던 포스팅
2021.04.08 - [개발 공부/코딩 테스트] - [LeetCode] - Climbing Stairs
[LeetCode] - Climbing Stairs
Memoization문제 sudo code n개의 계단을 올라간다 한걸음에 1 또는 2개 계단을 올라갈 수 있다 1 <= n <= 45 1 - [1] 2 - [1,1] [2] 3 - [1,1,1] [1,2] [2,1] 4 - [1,1,1,1] [1,1,2] [1,2,1] [2,1,1] [2,2] 5 - [..
devjun.tistory.com
class Solution {
public int climbStairs(int n) {
if (n <= 2) return n;
int one = 1;
int two = 2;
int answer = 0;
while(n > 2) {
answer = one + two;
one = two;
two = answer;
n--;
}
return answer;
}
}