File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ [๋ฌธ์ ํ์ด]
3
+ - nums ๋ฐฐ์ด ์์ 3๊ฐ์ง ์๋ฅผ ๋ํ์ ๋ 0์ด์ด์ผ ํ๋ค.
4
+ - ๋ํด์ง ์๋ค์ index๋ ๋ชจ๋ ๋ฌ๋ผ์ผ ํ๋ค.
5
+ - DFS (X)
6
+ StackOverflowError
7
+ - ํฌ ํฌ์ธํฐ (O)
8
+ time: O(N^2), space: O(N^2)
9
+
10
+ [ํ๊ณ ]
11
+ ํด์ค์ ๋ณด๊ณ ์ดํด๋ ํ๋๋ฐ ๋ค์ ํ ์ ์์๊น?
12
+ ์์ง ์คํฌ์ด ๋ถ์กฑํ ๊ฒ ๊ฐ๋ค..
13
+
14
+ */
15
+ class Solution {
16
+ public List <List <Integer >> threeSum (int [] nums ) {
17
+ Set <List <Integer >> answers = new HashSet <>();
18
+ Arrays .sort (nums );
19
+
20
+ for (int start = 0 ; start < nums .length ; start ++) {
21
+ int mid = start + 1 ;
22
+ int end = nums .length - 1 ;
23
+
24
+ while (mid < end ) {
25
+ int sum = nums [start ] + nums [mid ] + nums [end ];
26
+ if (sum == 0 ) {
27
+ List <Integer > answer = List .of (nums [start ], nums [mid ], nums [end ]);
28
+ answers .add (answer );
29
+ end --;
30
+ } else if (sum < 0 ) {
31
+ mid ++;
32
+ } else if (sum > 0 ) {
33
+ end --;
34
+ }
35
+ }
36
+ }
37
+ return new ArrayList <>(answers );
38
+ }
39
+ }
You canโt perform that action at this time.
0 commit comments