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. freckles

    题目描述: In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back ...

  2. VC++封装的时间类

    一.使用方法 首先要在工程中加入TimeNow.cpp和TimeNow.h文件 1.把.cpp与.h文件放在放在工程文件夹. 2.项目(progect)-->属性(properties)--&g ...

  3. TestLink测试管理工具的使用说明

    1. 创建项目: 主页左边的列表栏有 “Test Project management”的菜单,子菜单中有 “ create new test project”,通过它可以创建新的测试项目. 同时,菜 ...

  4. CentOS上升级gcc编译器使支持C++11

    首先向博主致敬,好的东西拿来共享了,用一下不错. https://blog.csdn.net/clirus/article/details/62424517 0. 目标  最近在学习c++11,我本机 ...

  5. Erlang-接口技术

    系统的构建一定会设计到简历接口,让他与不同的语言的应用程序之间简历系统的联系.这就叫做erlang的接口技术. 接口技术的三种实现方法: 1.让程序以外部操作系统进行的形式在Erlang虚拟机以外运行 ...

  6. 第k大元素

    在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注意 你可以交换数组 ...

  7. CPU查询

    请问:两路四核超线程=多少CPU??? 答案:16个 一.解释说明 两路=物理主机中,CPU的卡槽,槽位,这个无法增加,一个萝卜一个坑.就更内存卡槽一样,两个内存卡卡槽,a+b=总内存. 路=槽位=插 ...

  8. LVS DR模式搭建、keepalived+lvs

    1.LVS DR模式搭建 条件: 即三台机器,在同一内网. 编辑脚本文件:/usr/local/sbin/lvs_dr.sh #! /bin/bashecho 1 > /proc/sys/net ...

  9. psql备份和恢复(ubuntu)

    备份 sudo pg_dump -U  username  -f  filename.sql  dbname 恢复 psql -U username -f filename.sql dbname -- ...

  10. Python 回调函数

    什么是回调函数? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数: 这是官方的解释,理解上有点费 ...