File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ /**
4+ ํ์ด :
5+ ์ ๋ ฌ์ ํ ๋ค, ํ์ ์ ๊ธฐ์ค์ ์ผ๋ก ์ผ๊ณ , ํฌ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ๊ตฌํ๋ ๋ฐฉ์
6+
7+ ๋ณต์ก๋ ๊ณ์ฐ :
8+ ๋งค๊ฐ๋ณ์ nums์ ๊ธธ์ด -> N
9+ ์๊ฐ ๋ณต์ก๋ : O(N^2)
10+ ๊ณต๊ฐ ๋ณต์ก๋ : O(N^2)
11+ */
12+ class Solution {
13+ public List <List <Integer >> threeSum (int [] nums ) {
14+ Arrays .sort (nums );
15+ List <List <Integer >> result = new ArrayList <>();
16+ for (int i = 0 ; i < nums .length -2 ; i ++) {
17+
18+ // ์ด์ ๊ณผ ๊ฐ์ ๊ธฐ์ค์ ์ด ๋์ค๋ฉด, ์ค๋ณต์ผ๋ก result์ ๋ฑ๋ก๋ ์ ์์ผ๋ฏ๋ก, continue
19+ if (i > 0 && nums [i ] == nums [i - 1 ]){
20+ continue ;
21+ }
22+
23+ // ์ ๋ ฌํ ์ํฉ์์ ๊ธฐ์ค์ ์ด ์์๊ฐ ๋์ค๋ฉด, ์ฌ์ค์ ๋ถ๊ฐ๋ฅ
24+ if (nums [i ] > 0 ){
25+ break ;
26+ }
27+
28+ int targetSum = 0 - nums [i ];
29+
30+ int left = i + 1 ;
31+ int right = nums .length - 1 ;
32+
33+
34+ while (left < right ) {
35+ if ((nums [left ] + nums [right ]) > targetSum ) {
36+ right --;
37+ }else if ((nums [left ] + nums [right ]) < targetSum ) {
38+ left ++;
39+ }else {
40+ // ์ค๋ณต ๋ฐ์ดํฐ ์ถ๊ฐ์ ๋ํ if๋ฌธ
41+ if ((right == nums .length - 1 ) || (nums [right ] != nums [right +1 ])) {
42+ result .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
43+ }
44+ left ++;
45+ right --;
46+ }
47+ }
48+ }
49+ return result ;
50+ }
51+ }
You canโt perform that action at this time.
0 commit comments