LeetCode Blind 75 in Swift: Solving the "3Sum" Problem
Problem: 3Sum The 3Sum problem asks you to find all unique triplets in the array that give the sum of zero. Each triplet should be listed only once, even if multiple combinations can produce the same triplet. Example: Input: nums = [-1, 0, 1, 2, -1, -4] Output: [[-1, -1, 2], [-1, 0, 1]] Explanation: The two triplets with sum zero are [-1, -1, 2] and [-1, 0, 1]. Swift Solution class Solution { func threeSum(_ nums: [Int]) -> [[Int]] { let nums = nums.sorted() var result = [[Int]]() for i in 0.. 0 && nums[i] == nums[i - 1] { continue } var left = i + 1 var right = nums.count - 1 while left Explanation In this solution, the array is first sorted, which helps in efficiently finding triplets that sum to zero. For each element, we use the two-pointer technique to find pairs that together with the ...