File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 특이사항
3+ * 문제를 제대로 이해 못해서 풀이를 보며 이해했다.
4+ * 핵심은 중복 처리와 문제를 찾았을 때 종료가 아니라 다음 항목을 찾는 부분인 것 같다.
5+ * (추후 복습 예정)
6+ */
7+
8+ class Solution {
9+ public List <List <Integer >> threeSum (int [] nums ) {
10+
11+ // (1) ArrayList
12+ // 시간복잡도 : O(N^2), 공간복잡도 : O(N)
13+ List <List <Integer >> result = new ArrayList <>();
14+ Arrays .sort (nums );
15+
16+ for (int i = 0 ; i < nums .length ; i ++) {
17+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
18+
19+ int j = i + 1 ;
20+ int k = nums .length - 1 ;
21+
22+ while (j < k ) {
23+ int sumNum = nums [i ] + nums [j ] + nums [k ];
24+
25+ if (sumNum > 0 ) k --;
26+ else if (sumNum < 0 ) j ++;
27+ else {
28+ result .add (Arrays .asList (nums [i ], nums [j ], nums [k ]));
29+ j ++;
30+
31+ while (nums [j ] == nums [j - 1 ] && j < k ) j ++;
32+ }
33+ }
34+ }
35+
36+ return result ;
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments