File tree Expand file tree Collapse file tree 2 files changed +61
-1
lines changed Expand file tree Collapse file tree 2 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 1+ // 공간 복잡도를 줄인 2번째 버전
2+ class Solution {
3+ public int climbStairs (int n ) {
4+ int back = 1 ;
5+ int front = 2 ;
6+ int target = 0 ;
7+
8+ if (n == 1 ) return back ;
9+ if (n == 2 ) return front ;
10+
11+ for (int i = 1 ; i < n -1 ; i ++){
12+ target = front + back ;
13+ back = front ;
14+ front = target ;
15+ }
16+
17+ return target ;
18+ }
19+ }
20+
21+
22+ // 초기 버전
123class Solution {
224 public int climbStairs (int n ) {
325 int [] stairs = new int [n +1 ];
@@ -12,3 +34,9 @@ public int climbStairs(int n) {
1234 }
1335}
1436
37+ // 1 : 1
38+ // 2 : [1] + 1, 2 1+1 2 2개
39+ // 3 : [2-1] + 1, [2-2] + 1, [1] + 2 1+1+1 2+1 1+2 3개
40+ // 4 : [3-1] + 1, [3-2] + 1, [3-3] + 1 1+1+1+1 2+1+1 1+2+1 1+1+2 2+2 5개
41+ // 5 : 1+1+1+1+1 2+1+1+1 1+2+1+1 1+1+2+1 1+1+1+2 2+2+1 2+1+2 1+2+2 8개
42+
Original file line number Diff line number Diff line change 1+ // Map을 사용한 버전
2+ class Solution {
3+ public boolean isAnagram (String s , String t ) {
4+ if (s .length () != t .length ()) return false ;
5+
6+ Map <Character , Integer > alphabet = new HashMap <>();
7+
8+ for (char c : s .toCharArray ()){
9+ if (!alphabet .containsKey (c )){
10+ alphabet .put (c , 0 );
11+ }
12+ alphabet .put (c , alphabet .get (c ) + 1 );
13+ }
14+
15+ for (char c : t .toCharArray ()){
16+ if (!alphabet .containsKey (c )) return false ;
17+ if (alphabet .get (c ) == 0 ) return false ;
18+
19+ alphabet .put (c , alphabet .get (c )-1 );
20+ }
21+
22+ return true ;
23+ }
24+ }
25+
26+
27+ // 초기 버전
128class Solution {
229 public boolean isAnagram (String s , String t ) {
330 int [] character = new int [26 ];
@@ -17,4 +44,9 @@ public boolean isAnagram(String s, String t) {
1744 }
1845}
1946
20-
47+ /*
48+ Map, 배열 모두 평균시간복잡도는 O(1)이지만,
49+ 배열이 직접 접근 방식이고, Map은 Hash를 사용하여서 배열 보다는 시간이 더 걸린다
50+ 배열 사용시 4ms
51+ Map 사용 시 17ms
52+ */
You can’t perform that action at this time.
0 commit comments