Skip to content

Commit 689c778

Browse files
committed
climbing-stairs solution
1 parent 62f6663 commit 689c778

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

climbing-stairs/5YoonCheol.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
//피보나치 수열과 동일하다.
3+
public int climbStairs(int n) {
4+
// n < 3 일 때 n번째 계단에 오르는 방법은 n개
5+
if (n < 3) return n;
6+
// n >= 3 일 때는 (n-1)번째 계단에 오르는 방법 + (n-2)번째 계단에 오르는 방법
7+
int sec = 0;
8+
int prev = 1;
9+
int current = 2;
10+
for (int i = 3; i <= n; i++) {
11+
//i값이 증가할수록 (i-1)번째 값과 (i-2)번째 값이 각각 현재의 값과 이전 값으로 변경된다.
12+
sec = prev;
13+
prev = current;
14+
//현재 값은 (i-1)번째 값과 (i-2)번째 값의 합
15+
current = prev + sec;
16+
}
17+
return current;
18+
}
19+
}

0 commit comments

Comments
 (0)