Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0.
Constraints:
3 <= nums.length <= 3000-105 <= nums[i] <= 105
๋จ์๋ฌด์ํ๊ฒ ๋ฐ๋ณต๋ฌธ 3๊ฐ๋ก ํ๊ธด ํ์์ง๋ง, ์๊ฐ์ด๊ณผ๊ฐ ๋ด๋ค. ๋ฐ๋ณต๋ฌธ๋ง๊ณ ๋ ์๊ฐ๋๋ ์ ๊ทผ๋ฒ์ด ์์๋ค.
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
var answer: [[Int]] = []
for i in 0..<nums.count - 2 {
for k in 1..<nums.count - 1 {
for t in 2..<nums.count {
if i != k && i != t && k != t {
if nums[i] + nums[k] + nums[t] == 0 {
let array = [nums[i], nums[k], nums[t]].sorted()
if !answer.contains(array) {
answer.append(array)
}
}
}
}
}
}
return answer
}
}
๋ฐฐ์ด ์ฝ๋
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
var answer: [[Int]] = []
let array = nums.sorted()
for i in 0..<array.count - 2 {
if i > 0 && array[i] == array[i-1] {
continue
}
var left = i + 1
var right = array.count - 1
while left < right {
let sum = array[i] + array[left] + array[right]
if sum == 0 {
answer.append([array[i], array[left], array[right]])
while left < right && array[left] == array[left + 1] {
left += 1
}
while left < right && array[right] == array[right - 1] {
right -= 1
}
left += 1
right -= 1
} else if sum < 0 {
left += 1
} else {
right -= 1
}
}
}
return answer
}
}
์ ๊ทผ ๋ฐฉ์
1. array๋ฅผ sortํ๋ค.
2. array๋ฅผ ๋ฐ๋ณต๋ฌธ ๋๋ฆฐ๋ค.
4. ์ง๊ธ ์ธ๋ฑ์ค ๊ฐ์ ๋ค์ ์ธ๋ฑ์ค ์์์ ๊ฐ์ฅ ๋ฐฐ์ด์ ๊ฐ์ฅ ๋๋จ์ left, right๋ฅผ ๋๋ค.
3. ์ง๊ธ ์ธ๋ฑ์ค์ ์์๊ฐ, ๋ฐ๋ก ์ด์ ์์์ ๊ฐ์ผ๋ฉด ๊ฑด๋๋ด๋ค.
4-1. sum ์ด 0์ด๋ผ๋ฉด, ๋ฐฐ์ด์ appendํ๋ค
array[left] == array[left+1] ์ผ ๋๊น์ง left๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์ํจ๋ค.
array[right] == array[right-1] ์ผ๋๊น์ง right๋ฅผ ์ผ์ชฝ์ผ๋ก ์ด๋์ํจ๋ค.
์ค๋ณต ์ง์ ์ ์ฐพ์์ผ๋, ๊ทธ ์ง์ ์์ left์ right๋ฅผ ํ ์นธ์ฉ ์ด๋์์ผ ์๋ก์ด ์กฐํฉ์ ์ฐพ๋๋ค.
4-2. sum์ด 0๋ณด๋ค ์์ผ๋ฉด, left๋ฅผ ํ ์นธ ์ฎ๊ฒจ์ ์ข ๋ sum์ ํฌ๊ฒ ๋ง๋ ๋ค.
4-3. sum์ด 0๋ณด๋ค ํฌ๋ฉด, right๋ฅผ ํ ์นธ ์ฎ๊ฒจ์ ์ข ๋ sum์ ์ ๊ฒ ๋ง๋ ๋ค.
ํฌํฌ์ธํฐ ์ ๊ทผ๋ฐฉ์
- ์ ์:
- ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ด๋ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ ์๊ณ ๋ฆฌ์ฆ ํจ๋ฌ๋ค์์ ๋๋ค.
- ์ฃผ๋ก ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ์ฌ์ฉ๋๋ฉฐ, ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ ์์ ์ฐพ๋ ๋ฐ ํจ๊ณผ์ ์ ๋๋ค.
- ์๋ ๋ฐฉ์:
- ๋ณดํต ํ๋์ ํฌ์ธํฐ๋ ๋ฐฐ์ด์ ์์, ๋ค๋ฅธ ํ๋๋ ๋์์ ์์ํฉ๋๋ค.
- ๋ ํฌ์ธํฐ๋ฅผ ์กฐ๊ฑด์ ๋ฐ๋ผ ์์ง์ด๋ฉด์ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์ต๋๋ค.
- ์ฌ์ฉ ์ฌ๋ก:
- ๋ ์์ ํฉ ๋ฌธ์ (์: ์ฃผ์ด์ง ํฉ์ ๋ง๋๋ ๋ ์ ์ฐพ๊ธฐ)
- ์ธ ์์ ํฉ ๋ฌธ์ (3Sum)
- ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ ๋ฌธ์
- ํฐ๋ฆฐ๋๋กฌ ํ์ธ
- ์ฅ์ :
- ์๊ฐ ๋ณต์ก๋ ๊ฐ์ : ๋๋ถ๋ถ์ ๊ฒฝ์ฐ O(n^2)์์ O(n)์ผ๋ก ์ค์ผ ์ ์์ต๋๋ค.
- ๊ณต๊ฐ ํจ์จ์ฑ: ์ถ๊ฐ ๊ณต๊ฐ์ ๊ฑฐ์ ์ฌ์ฉํ์ง ์์ต๋๋ค (in-place ์๊ณ ๋ฆฌ์ฆ).
- ํต์ฌ ์์ด๋์ด:
- ์ ๋ ฌ๋ ๋ฐ์ดํฐ์ ํน์ฑ์ ํ์ฉํฉ๋๋ค.
- ๋ถํ์ํ ์กฐํฉ์ ํจ๊ณผ์ ์ผ๋ก ์ ๊ฑฐํฉ๋๋ค.
- ๊ฐ ๋จ๊ณ์์ ๊ฒ์ ๋ฒ์๋ฅผ ์ขํ๋๊ฐ๋๋ค.
'Algorithm > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋งค์ผ LeetCode ํ๊ธฐ | Maximum Product Subarray (0) | 2024.08.23 |
---|---|
๋งค์ผ LeetCode ํ๊ธฐ | Maximum Subarray (0) | 2024.08.21 |
๋งค์ผ LeetCode ํ๊ธฐ | Number of 1 bits (0) | 2024.08.19 |
๋งค์ผ LeetCode ํ๊ธฐ | Sum of Two Integers (0) | 2024.08.18 |
๋งค์ผ LeetCode ํ๊ธฐ | Product of Array Except Self (0) | 2024.08.15 |