classSolution(object): defthreeSum(self, nums): nums.sort() result = [] length = len(nums) for i inrange(length - 2): if i > 0and nums[i] == nums[i - 1]: continue left, right = i + 1, length - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif total > 0: right -= 1 else: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 return result
classSolution: defthreeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() result = [] length = len(nums) for i inrange(length - 2): if i > 0and nums[i] == nums[i - 1]: continue left, right = i + 1, length - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif total > 0: right -= 1 else: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 return result
classSolution { functhreeSum(_nums: [Int]) -> [[Int]] { var result = [[Int]]() let length = nums.count let sortedNums = nums.sorted() for i in0..<length -2 { if i ==0|| (i >0&& sortedNums[i] != sortedNums[i -1]) { var left = i +1 var right = length -1 let target =-sortedNums[i] while left < right { let sum = sortedNums[left] + sortedNums[right] if sum == target { result.append([sortedNums[i], sortedNums[left], sortedNums[right]]) while left < right && sortedNums[left] == sortedNums[left +1] { left +=1 } while left < right && sortedNums[right] == sortedNums[right -1] { right -=1 } left +=1 right -=1 } elseif sum < target { left +=1 } else { right -=1 } } } } return result } }
classSolution { funthreeSum(nums: IntArray): List<List<Int>> { val result: MutableList<List<Int>> = mutableListOf() val length = nums.size val sortedNums = nums.sorted() for (i in0 until length - 2) { if (i == 0 || (i > 0 && sortedNums[i] != sortedNums[i - 1])) { var left = i + 1 var right = length - 1 val target = -sortedNums[i] while (left < right) { val sum = sortedNums[left] + sortedNums[right] when { sum == target -> { result.add(listOf(sortedNums[i], sortedNums[left], sortedNums[right])) while (left < right && sortedNums[left] == sortedNums[left + 1]) left++ while (left < right && sortedNums[right] == sortedNums[right - 1]) right-- left++ right-- } sum < target -> left++ else -> right-- } } } } return result } }
functhreeSum(nums []int) [][]int { var result [][]int sort.Ints(nums) length := len(nums) for i := 0; i < length-2; i++ { if i == 0 || (i > 0 && nums[i] != nums[i-1]) { left, right := i+1, length-1 target := -nums[i] for left < right { sum := nums[left] + nums[right] if sum == target { result = append(result, []int{nums[i], nums[left], nums[right]}) for left < right && nums[left] == nums[left+1] { left++ } for left < right && nums[right] == nums[right-1] { right-- } left++ right-- } elseif sum < target { left++ } else { right-- } } } } return result }
# @param {Integer[]} nums # @return {Integer[][]} defthree_sum(nums) nums.sort! result = [] (0..nums.length - 2).each do |i| if i == 0 || (i > 0 && nums[i] != nums[i - 1]) left = i + 1 right = nums.length - 1 target = -nums[i] while left < right sum = nums[left] + nums[right] if sum == target result << [nums[i], nums[left], nums[right]] left += 1while left < right && nums[left] == nums[left + 1] right -= 1while left < right && nums[right] == nums[right - 1] left += 1 right -= 1 elsif sum < target left += 1 else right -= 1 end end end end result end
objectSolution{ defthreeSum(nums: Array[Int]): List[List[Int]] = { val sortedNums = nums.sorted val result = scala.collection.mutable.ListBuffer[List[Int]]() for (i <- 0 until sortedNums.length - 2) { if (i == 0 || (i > 0 && sortedNums(i) != sortedNums(i - 1))) { var left = i + 1 var right = sortedNums.length - 1 val target = -sortedNums(i) while (left < right) { val sum = sortedNums(left) + sortedNums(right) if (sum == target) { result += List(sortedNums(i), sortedNums(left), sortedNums(right)) while (left < right && sortedNums(left) == sortedNums(left + 1)) left += 1 while (left < right && sortedNums(right) == sortedNums(right - 1)) right -= 1 left += 1 right -= 1 } elseif (sum < target) { left += 1 } else { right -= 1 } } } } result.toList } }
implSolution { pubfnthree_sum(nums: Vec<i32>) ->Vec<Vec<i32>> { letmut result = Vec::new(); letmut nums = nums; nums.sort(); foriin0..nums.len() - 2 { if i == 0 || (i > 0 && nums[i] != nums[i - 1]) { letmut left = i + 1; letmut right = nums.len() - 1; lettarget = -nums[i]; while left < right { letsum = nums[left] + nums[right]; if sum == target { result.push(vec![nums[i], nums[left], nums[right]]); while left < right && nums[left] == nums[left + 1] { left += 1; } while left < right && nums[right] == nums[right - 1] { right -= 1; } left += 1; right -= 1; } elseif sum < target { left += 1; } else { right -= 1; } } } } result } }