1. ๋ฌธ์ : https://leetcode.com/problems/3sum/
2. ํ์ด
์ ๋ ฌํ ํฌํฌ์ธํฐ ์ฌ์ฉ
(๋ฐฐ์ด์ด ์ ๋ ฌ๋์ด์๋ค๋ ๊ฒ์ ์ด์ฉํด์ผํจ)
3. ์ฝ๋
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i-1]) continue;
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right] + nums[i];
if (sum == 0) {
res.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++;
right--;
while (nums[left] == nums[left - 1] && left < right) left++;
while (nums[right] == nums[right + 1] && left < right) right--;
} else if (sum > 0) {
right--;
} else {
left++;
}
}
}
return res;
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ (์๋ฐ java) (0) | 2023.05.15 |
[BOJ 1806] ๋ถ๋ถํฉ (java) (0) | 2023.04.05 |
[LeetCode] Container With Most Water (0) | 2023.03.28 |
[BOJ 14719] ๋น๋ฌผ (0) | 2022.06.26 |
์ต๊ทผ๋๊ธ