网站建设4038gzs,公司的网站,网站建设流程有哪七步,江苏省建设教育协会网站首页给你一个整数数组 nums #xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k #xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请 你返回所有和为 0 且不重复的三元组。 注意#xff1a;答案中不可以包含重复的三元组。 示例 1判断是否存在三元组 [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 - 1 0 5 10^5 105 nums[i] 1 0 5 10^5 105 这个问题其实不难对于我来说还是那个经验在你的想象力不够好的情况下拿一个例子来求解很多细节问题就清楚了。
我想到了先进行排序然后抽取一个数再遍历数组找出另外两个数使三个数和为零但是细节想失误了认为从一个数组中找出所有和为指定值的组合需要 O ( n 2 ) O(n^2) O(n2), 实际需要 O ( n ) O(n) O(n), 关键还是没动手。
class Solution:def threeSum(self, nums: list) - list:#升序排序nums sorted(nums, keylambda x:x, reverseFalse)if nums[0] 0 or nums[len(nums) - 1] 0:return []if min(nums) 0 and max(nums) 0:return [[0,0,0]]result []for i in range(len(nums)):#不重复if (i 0) and nums[i-1] nums[i] or nums[i] 0:continuenum nums[i]t self.twoSum(nums[i 1:], num)if t:result.extend(t)return resultdef twoSum(self, nums, value):target -valuer []p, q 0, len(nums) - 1while p q:t nums[p] nums[q]if t target:r.append([value, nums[p], nums[q]])while p q:if nums[p1] ! nums[p]:p 1breakelse:p 1while p q:if nums[q-1] ! nums[q]:q - 1breakelse:q - 1elif t target:p 1else:q - 1if len(r) 0:return Noneelse:return r
代码的第12,13行主要针对排序后的数组有连续的相同数字可以思考下为什么这么写。 文章转载自: http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn