File tree Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * 시간복잡도 : O(N^2)
3
+ * 공간복잡도 : O(1)
4
+ * */
5
+ import java .util .*;
6
+
7
+ class Solution {
8
+ public List <List <Integer >> threeSum (int [] nums ) {
9
+ List <List <Integer >> result = new ArrayList <>();
10
+ Arrays .sort (nums );
11
+
12
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
13
+ if (i > 0 && nums [i ] == nums [i - 1 ]) {
14
+ continue ;
15
+ }
16
+
17
+ int left = i + 1 ;
18
+ int right = nums .length - 1 ;
19
+
20
+ while (left < right ) {
21
+ int sum = nums [i ] + nums [left ] + nums [right ];
22
+
23
+ if (sum == 0 )
24
+ {
25
+ // 합이 0인 경우 결과에 추가
26
+ result .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
27
+
28
+ // 중복된 값 건너뛰기
29
+ while (left < right && nums [left ] == nums [left + 1 ])
30
+ left ++;
31
+
32
+ while (left < right && nums [right ] == nums [right - 1 ])
33
+ right --;
34
+
35
+ left ++;
36
+ right --;
37
+ }
38
+ else if (sum < 0 )
39
+ left ++;
40
+ else
41
+ right --;
42
+
43
+ }
44
+ }
45
+ return result ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ 시간복잡도 : O(n)
3
+ 공간복잡도 : O(n)
4
+ */
5
+ class Solution {
6
+ public int climbStairs (int n ) {
7
+ if (n <= 2 ) return n ;
8
+
9
+ int [] dp = new int [n + 1 ];
10
+ dp [1 ] = 1 ;
11
+ dp [2 ] = 2 ;
12
+
13
+ for (int i = 3 ; i <= n ; i ++)
14
+ dp [i ] = dp [i - 1 ] + dp [i - 2 ];
15
+
16
+ return dp [n ];
17
+
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ 시간복잡도 : O(N)
3
+ 공간복잡도 : O(1)
4
+ */
5
+
6
+ class Solution {
7
+ public boolean isAnagram (String s , String t ) {
8
+ if (s .length () != t .length ()) return false ;
9
+
10
+ int [] character = new int [26 ];
11
+
12
+ for (int i = 0 ; i < s .length (); i ++) {
13
+ character [s .charAt (i ) - 'a' ]++;
14
+ character [t .charAt (i ) - 'a' ]--;
15
+ }
16
+
17
+
18
+ for (int num : character ) {
19
+ if (num != 0 )
20
+ return false ;
21
+ }
22
+
23
+ return true ;
24
+
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments