File tree 2 files changed +62
-0
lines changed
2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ i번째 칸에 가는 방법은 (1) i-2번째 칸에서 2칸을 점프하거나 (2) i-1번째 칸에서 1칸을 점프하는 2가지 방법 뿐입니다. (MECE함)
3
+ 따라서, (i번째 칸에 가는 경우의 수) = (i-2번째 칸에 가는 경우의 수) + (i-1번째 칸에 가는 경우의 수)
4
+
5
+ Runtime: 0 ms (Beats: 100.00%)
6
+ Time Complexity: O(n)
7
+
8
+ Memory: 40.47 MB (Beats: 36.79%)
9
+ Space Complexity: O(1)
10
+ */
11
+
12
+ class Solution {
13
+ public int climbStairs (int n ) {
14
+ if (n == 1 ) {
15
+ return 1 ;
16
+ } else if (n == 2 ) {
17
+ return 2 ;
18
+ } else {
19
+ int prev2 = 1 ;
20
+ int prev1 = 2 ;
21
+ int cur = 0 ;
22
+ for (int i = 3 ; i <= n ; i ++) {
23
+ cur = prev2 + prev1 ;
24
+ prev2 = prev1 ;
25
+ prev1 = cur ;
26
+ }
27
+ return cur ;
28
+ }
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ s와 t는 알파벳 소문자로만 이루어지므로, 카운팅을 위해 26개의 고정된 key를 사용하면 충분하고, 배열이 가장 간단하고 적합하다고 생각함
3
+
4
+ Runtime: 4 ms (Beats: 76.59%)
5
+ Time Complexity: O(n)
6
+
7
+ Memory: 43.04 MB (Beats: 78.65%)
8
+ Space Complexity: O(1)
9
+ */
10
+
11
+ class Solution {
12
+ public boolean isAnagram (String s , String t ) {
13
+ if (s .length () != t .length ())
14
+ return false ;
15
+
16
+ int [] cnt = new int [26 ];
17
+
18
+ for (int i = 0 ; i < s .length (); i ++) {
19
+ cnt [s .charAt (i ) - 'a' ]++;
20
+ }
21
+ for (int i = 0 ; i < t .length (); i ++) {
22
+ cnt [t .charAt (i ) - 'a' ]--;
23
+ }
24
+
25
+ for (int i = 0 ; i < 26 ; i ++) {
26
+ if (cnt [i ] != 0 )
27
+ return false ;
28
+ }
29
+
30
+ return true ;
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments