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. golang结构体、接口、反射

    struct结构体 struct用来自定义复杂数据结构,可以包含多个字段属性,可以嵌套; go中的struct类型理解为类,可以定义方法,和函数定义有些许区别; struct类型是值类型. struc ...

  2. 2017年3月30日15:00:19 fq以后的以后 动态代理

    代理与继承,组合不同的是,继承是继承父类特性,组合是拼装组合类的特性,代理是使用代理类的指定方法并可以做自定义. 静态类是应用单个类,当代理的类数量较多时可用动态代理,动态代理在概念上很好理解 htt ...

  3. 提供一个Java字符串转整型数组的方法

    package edu.yuliang.Data_Structure_Basics; import java.util.Scanner; public class new_string { publi ...

  4. 2.常用adb命令的使用

    使用电脑连接手机,查看手机的唯一编号,如果是模拟器,就是显示地址和端口号: adb devices 使用adb安装app应用: adb install apk路径和包名 -r 允许覆盖安装 -s 将a ...

  5. erlang工作总结

    总结下自己在做erlang的经验 1.不管什么样的情况下,一定要关注好函数的返回值再来使用,不知道返回值盲目的使用的话,不仅不能达到目标,而且不存在代码/报错提醒.得不偿失. 2.构思好自己的想法,定 ...

  6. 获取真实ip三个方法

    方法一: /** * 获取客户端IP地址 * * @return string */function get_client_ip(){ if (getenv("HTTP_CLIENT_IP& ...

  7. HDU 2062:Subset sequence(思维)

    Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. 唯一分解定理(以Minimun Sum LCM UVa 10791为例)

    唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...

  9. windows下使用kafka的常用命令

    参考文档: https://blog.csdn.net/evankaka/article/details/52421314 http://orchome.com/6 1 启动zookeeper cmd ...

  10. github/gitee使用办法

    github/gitee只要添加SSH公钥都是可以连接上的 比如把某个文件上传gitee 首先肯定要有权限    否则会一直提醒failed伤心心 接下来说常用语句 git config --list ...