# threeSum LeeCode --- 给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。 注意:答案中不可以包含重复的三元组。 示例: 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: ```shell= [ [-1, 0, 1], [-1, -1, 2] ] ``` ```golang= func threeSum(nums []int) [][]int { sort.Ints(nums) result := [][]int{} for i := 0; i < len(nums)-1; i++ { // 过滤 去重 if i > 0 && nums[i] == nums[i-1] { continue } j := i + 1 z := len(nums) - 1 for z > j { a := nums[i] b := nums[j] c := nums[z] if a+b+c > 0 { z-- } else if a+b+c < 0 { j++ } else { item := []int{a, b, c} result = append(result, item) for j < z && nums[j] == nums[j+1] { j++ } for j < z && nums[z] == nums[z-1] { z-- } j++ z-- } } } return result } ``` 题目来源 转载自:https://leetcode-cn.com/problems/3sum 来源:力扣(LeetCode) ###### tags: `LeeCode`