漯河市郾城区网站建设,怀化住建部网站,厦门关键词优化软件,西宁网站建设电话给你一个整数数组 nums #xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k #xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请
你返回所有和为 0 且不重复的三元组。
注意#xff1a;答案中不可以包含重复的三元组。
示例 …给你一个整数数组 nums 判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k 同时还满足 nums[i] nums[j] nums[k] 0 。请
你返回所有和为 0 且不重复的三元组。
注意答案中不可以包含重复的三元组。
示例 1
输入nums [-1,0,1,2,-1,-4] 输出[[-1,-1,2],[-1,0,1]] 解释 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 。 不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。 注意输出的顺序和三元组的顺序并不重要。 示例 2
输入nums [0,1,1] 输出[] 解释唯一可能的三元组和不为 0 。 示例 3
输入nums [0,0,0] 输出[[0,0,0]] 解释唯一可能的三元组和为 0 。
提示
3 nums.length 3000 -105 nums[i] 105 来源力扣LeetCode 链接https://leetcode.cn/problems/3sum 题解
思路 开头容易让人想到三层循环hash去重但时间复杂度和空间消耗比较大 使用排序双指针达到去除重复值的作用 还要明确几个条件 数组为空或数组长度小于三排序后最小值为正数就不可能存在想加为0 的情况有连续重复的数字要往后移这样就能保证去重的效果若三个数组位的值想加等于0还要考虑他们之后的情况是否又有重复的值若三个数组位的值相加小于零要考虑将L向后移一位若三个数组位的值相加大于零要考虑将R向前移一位
class Solution {public static ListListInteger threeSum(int[] nums) {ListListInteger ansnew ArrayList();int lennums.length;if(numsnull||len3){return ans;//如果数组的长度小于3 或者数组为空返回值为空}//对数组排序Arrays.sort(nums);//时间复杂度为nlognfor(int i0;ilen;i){if(nums[i]0){break;}//最小的都大于0不可能存在加起来等于0的情况if(i0nums[i]nums[i-1]) continue;//相邻值相同去重int Li1;int Rlen-1;while(LR){int sum0;sumnums[i]nums[R]nums[L];if(sum0){ans.add(Arrays.asList(nums[i],nums[L],nums[R]));while(LRnums[L]nums[L1]) L;while(LRnums[R]nums[R-1]) R--;L;R--;}else if(sum0) L;else if(sum0) R--;}}return ans;}
}
文章转载自: http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.lmmkf.cn.gov.cn.lmmkf.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn