24 手表网站,网站建设学校培训学校,建设项目竣工验收公示网站,hfs网络文件服务器可以做网站1. 三数之和 M
:::details 给你一个包含 n 个整数的数组 nums#xff0c;判断 nums 中是否存在三个元素 a#xff0c;b#xff0c;c #xff0c;使得 a b c 0 #xff1f;请你找出所有和为 0 且不重复的三元组。 注意#xff1a;答案中不可以包含重复的三元组。 示例…1. 三数之和 M
:::details 给你一个包含 n 个整数的数组 nums判断 nums 中是否存在三个元素 abc 使得 a b c 0 请你找出所有和为 0 且不重复的三元组。 注意答案中不可以包含重复的三元组。 示例 1 输入nums [-1,0,1,2,-1,-4] 输出[[-1,-1,2],[-1,0,1]] 示例 2 输入nums [] 输出[] 示例 3 输入nums [0] 输出[] 提示 0 nums.length 3000 -105 nums[i] 105 来源力扣LeetCode 链接https://leetcode.cn/problems/3sum 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 解题思路 因为题目要求输出的是value而不是index所以为了简单起见我们上来先来一个排序 然后利用双指针两头往中间靠 注意去重 package mainimport (fmtsort
)func threeSum(nums []int) [][]int {sort.Ints(nums)// fmt.Println(nums)res : [][]int{}n : len(nums)for i : 0; i n-2; i {l, r : i1, n-1if nums[i] 0 {break}if i 0 nums[i] nums[i-1] {// 去重continue}for l r {in, ln, rn : nums[i], nums[l], nums[r]if inlnrn 0 {res append(res, []int{in, ln, rn})for l r nums[l] ln {l}for l r nums[r] rn {r--}} else if inlnrn 0 {l} else {r--}}}return res
}func main() {nums : []int{-1, 0, 1, 2, -1, -4}fmt.Println(threeSum(nums))
}
:::
2. 四数之和 M
:::details 给你一个由 n 个整数组成的数组 nums 和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] 若两个四元组元素一一对应则认为两个四元组重复 0 a, b, c, d n a、b、c 和 d 互不相同 nums[a] nums[b] nums[c] nums[d] target 你可以按 任意顺序 返回答案 。 示例 1 输入nums [1,0,-1,0,-2,2], target 0 输出[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] 示例 2 输入nums [2,2,2,2,2], target 8 输出[[2,2,2,2]] 提示 1 nums.length 200 -109 nums[i] 109 -109 target 109 来源力扣LeetCode 链接https://leetcode.cn/problems/4sum 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 func fourSum(nums []int, target int) [][]int {sort.Ints(nums)// fmt.Println(nums)n : len(nums)res : [][]int{}for i : 0; i n-3; i {/*** 因为target可以是负数所以不能这么剪枝* 例如 [-5,-4,-3,-2,1,5,4,2] target -14*/// if nums[i] target {// break// }// 去重 [a,a,x,y]if i 0 nums[i] nums[i-1] {continue}for j : i 1; j n-2; j {in, jn : nums[i], nums[j]twoSum : in jnif j i1 nums[j] nums[j-1] {continue}l, r : j1, n-1for l r {ln, rn : nums[l], nums[r]temp : twoSum ln rnif temp target {res append(res, []int{in, jn, ln, rn})// 去重for l r nums[l] ln {l}for l r nums[r] rn {r--}} else if temp target {for l r nums[l] ln {l}} else {for l r nums[r] rn {r--}}}}}return res
}:::