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. lua调用方法错误

    self.sendMsg(json.encode(info),) self:sendMsg(json.encode(info),) 两个符号就差了一个点,引发的问题确实app崩溃,在这个方法中传的参数 ...

  2. React Native 继续学习

    下一个项目公司也打算使用react native.大致看了下原型设计,写几个小demo先试试水.特此记录下. 1.微信及朋友圈分享.QQ及朋友圈分享,微博分享,微信支付,支付宝支付. 2.导航条渐隐 ...

  3. C#如何运行外部程序(打开可执行程序):ShellExcute和Process

    最近的任务用到C#来调用C++内核程序,也就是C++编译运行后生成的.exe文件.网搜了一下C#中运行外部程序大致有两种方法,在此稍作总结: (1)使用API函数ShellExcute 添加引用 us ...

  4. Go语言for循环语句

    for循环语句解释与使用 首先我们定义sum:=0 格式:for i:=1;i<=100;i++{ sum+=i } for 循环100次,首先执行i=1,然后判断i<100,再执行sum ...

  5. linux下不能拼通www.baidu.com

    1.打开虚拟机,通过命令修改内容如下 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.将信息修改如下: 3.ping www.baidu.com 查看是否 ...

  6. cordova文件传输系统插件使用:cordova-plugin-file-transfer

    1. 添加插件:cordova plugin add cordova-plugin-file-transfer 2. 调用方法: var fileTransfer = new FileTransfer ...

  7. url集合

    restful方面 Java后台框架篇--Spring与Restful风格API接口开发 https://blog.csdn.net/hello_worldee/article/details/781 ...

  8. CANVAS笔记

    bglayer.add(bgimage) /*后面的层会覆盖前面的,所以要放在上面的,就要后面才添加!*/ bglayer.add(this.shape1) Layer.destroy() layer ...

  9. word embeddding和keras中的embedding

    训练好的词向量模型被保存下来,该模型的本质就是一个m*n的矩阵,m代表训练语料中词的个数,n代表训练时我们设定的词向量维度.当我们训练好模型后再次调用时,就可以从该模型中直接获取到对应词的词向量. 通 ...

  10. 1-hadoop、mr

    1.HDFS的优缺点: 优点: ① 高容错 ② 可扩展 ③ 适合大文件存储 ④ 可构建在廉价的机器上 缺点: ① 高延迟 ② 文件不能修改 ③ 不适合小文件存储 2.HDFS架构(类似于文件系统): ...