We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 62f6663 commit 689c778Copy full SHA for 689c778
climbing-stairs/5YoonCheol.java
@@ -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