class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ls = new LinkedList<>(); for (int i = 0; i < nums.length - 2; i++) {
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) { // 跳过可能重复的答案 int l = i + 1, r = nums.length - 1, sum = 0 - nums[i];
while (l < r) {
if (nums[l] + nums[r] == sum) {
ls.add(Arrays.asList(nums[i], nums[l], nums[r]));
while (l < r && nums[l] == nums[l + 1]) l++;
while (l < r && nums[r] == nums[r - 1]) r--;
l++;
r--;
} else if (nums[l] + nums[r] < sum) {
while (l < r && nums[l] == nums[l + 1]) l++; // 跳过重复值
l++;
} else {
while (l < r && nums[r] == nums[r - 1]) r--;
r--;
}
}
}
}
return ls;
}
}

使用python实现:

 class Solution:
def threeSum(self, nums: 'List[int]') -> 'List[List[int]]':
nums = sorted(nums)
res = []
n = len(nums)
for i in range(n-2):
if i == 0 or nums[i] != nums[i-1]:
l,r,sums = i + 1,n-1,0-nums[i]
while l < r:
if nums[l] + nums[r] == sums:
res.append([nums[i],nums[l],nums[r]])
while l < r and nums[l] == nums[l+1]:
l += 1
while l < r and nums[r] == nums[r-1]:
r -= 1
l += 1
r -= 1
elif nums[l] + nums[r] < sums:
while l < r and nums[l] == nums[l+1]:
l += 1
l += 1
else:
while l < r and nums[r] == nums[r-1]:
r -= 1
r -= 1
return res

下面补充另一种思路,使用python实现,是根据leetcode167 twonum II的思路来做的,效率比上面的方法要高一些,代码如下:

 class Solution:
def towSum(self,numbers,target):
i = 0
j = len(numbers) - 1 ary = list()
while i < j:
sums = numbers[i] + numbers[j]
#print(str(i)+','+str(j)+'->'+str(sums))
if sums == target:
#return numbers[i],numbers[j]
ary.append([numbers[i],numbers[j]])
while i < j and numbers[i]==numbers[i+1]:
i+=1
i+=1
elif sums < target:
i+=1
else:
j-=1
return ary def threeSum(self, nums: 'List[int]') -> 'List[List[int]]':
result = list() sortedlist = sorted(nums) zlist = list(filter(lambda x:x==0,sortedlist))
if len(zlist) >= 3:
result.append([0,0,0]) nlist = list(filter(lambda x:x<0,sortedlist))
nset = set(nlist) plist = list(filter(lambda x:x>=0,sortedlist))
pset = set(plist)
for target in nset:
aa = self.towSum(plist,target*-1)
for a in aa:
a1 = a[0]
a2 = a[1]
clist = [a1,a2,target]
result.append(clist) for target in pset:
aa = self.towSum(nlist,target*-1)
for a in aa:
a1 = a[0]
a2 = a[1]
clist = [a1,a2,target]
result.append(clist) return result

leetcode15的更多相关文章

  1. [array] leetCode-15. 3Sum-Medium

    leetCode-15. 3Sum-Medium descrition Given an array S of n integers, are there elements a, b, c in S ...

  2. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  3. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  4. LeetCode15 3Sum

    题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  5. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  6. LeetCode1-5题

    1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  7. leetcode15—3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  8. LeetCode15. 三数之和

    15. 三数之和 描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  9. LeetCode15.三数之和 JavaScript

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

随机推荐

  1. React项目新手指南

    对于程序员而言:驼峰和下划线之间是一场宗派战争:大括号是否换行会成为一种党派:逗号写在行尾还是行首的人来自不同星球…… 然而,无规矩不成方圆,任何一个团队,要想有高质量的产出,第一步必须要对一些基本的 ...

  2. itextsharp报错PdfReader not opened with owner password

    itextSharp读取Pdf时报错:PdfReader not opened with owner password 报错原因:pdf文件被用户加密了. 解决办法:在创建pdfReader实例后,加 ...

  3. Cordova使用极光推送的方法

    1.在极光推送官网注册账号.应用,注意注册的包名要和app的包名一致 2.添加插件 cordova plugin add jpush-phonegap-plugin --variable APP_KE ...

  4. .NET并行计算和并发5:多线程编程一般指导性原则

    使用多线程时要考虑以下准则: 不要使用 Thread.Abort 终止其他线程. 对另一个线程调用 Abort 无异于引发该线程的异常,也不知道该线程已处理到哪个位置. 不要使用 Thread.Sus ...

  5. 本地Debug Asp.net MVC 无法加载css与js

    运行一个从网上download的一个MVC项目,运行的时候无法显示样式,js也报错. 检查路径也没有问题,后来在配置中把其中的一段配置去掉 <staticContent> <!-- ...

  6. javax/servlet/jsp/jstl/core/Config

    javax/servlet/jsp/jstl/core/Config springmvc出现的问题. 尝试了各种jar,问题依旧. DispatcherServlet配置如下. <bean id ...

  7. python retrying retry

    在写业务时, 有个业务需要在return某种情况下,进行重试. 代码类似于下面 from retrying import retry def verify(self): try: return sel ...

  8. vs+qt 运行过程出现cannot run rc.exe

    刚开始,我按照网上的一堆教程在qt creator中设置了各种东西,设置完以后,运行时出现cannot run rc.exe,根据百度,将C:\Program Files (x86)\Windows ...

  9. PHP处理上传文件信息数组中的文件类型 正确获取

    PHP处理上传文件信息数组中的文件类型$_FILES['type']由客户端浏览器提供,有可能是黑客伪造的信息,请写一个函数来确保用户上传的图像文件类型真实可靠 如果是一般文件则通过 mime_con ...

  10. java应用:向用户注册的邮箱发送邮件

    实现功能 忘记密码,注册成功等向用户发送验证码信息或注册信息. 业务流程 忘记密码: 1.验证邮箱是否注册过: 2.向邮箱发送验证码: 3.验证验证码是否正确: 4.重新设置密码: 我这里着重介绍发送 ...