23.3Sum(三数和为零)
Level:
Medium
题目描述:
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
思路分析:
求数组中三数和为零的情况,首先我们将数组进行排序,然后遍历数组,设置两个指针left和right,访问到节点i的时候,将left设置为i+1,right设置为nums.length-1。然后看nums[left]和nums[right]的和是否为-nums[i],如果是,那么就找到一组满足要求的解,如果不能则移动left和right,直到相等。
注意要避免重复的情况
代码:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>>res=new ArrayList<>();
if(nums==null||nums.length<3)
return res;
Arrays.sort(nums); //进行排序
for(int i=0;i<nums.length;i++){
int towsum=-nums[i];
int left=i+1;
int right=nums.length-1;
while(left<right){
if(nums[left]+nums[right]==towsum){
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
left++;
while(left<nums.length&&nums[left]==nums[left-1])
left++; //避免出现重复情况
right--;
while(right>=0&&nums[right]==nums[right+1])
right--; //避免出现重复情况
}
else if(nums[left]+nums[right]<towsum){
left++;
}
else{
right--;
}
}
while(i<nums.length-1&&nums[i]==nums[i+1])
i++; //避免出现重复情况
}
return res;
}
}
23.3Sum(三数和为零)的更多相关文章
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [Leetcode] 3sum 三数和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [LeetCode] 15. 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- [LintCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [Lintcode 3sum]三数之和(python,二分)
题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- Leetcode15.3Sum三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
随机推荐
- 一次 Mysql 字符集的报错,最后让我万马奔腾!!!
wuba---深圳---龙岗周边----3000元--------- wuba---深圳---龙岗周边----5000元--------- wuba---深圳---龙岗周边----8000元----- ...
- mysql如何进行以=分割的字符串的拆分
SUBSTRING_INDEX(str, delim, count) str: 要处理的字符串 delim: 分割符 count: 计数 如果为正数,则从左开始数,如果为负数,则从右开始数 使用示例: ...
- Linux 常用命令(转)
转自:https://www.cnblogs.com/gaojun/p/3359355.html 1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文 ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 调用req.getParameter方法出现中文乱码(全是问号???)
在java开发中,如果编码配置不统一,很容易出现中文乱码的情况,这里就记录下自己遇到的调用req.getParameter方法出现中文乱码,并解决这一情况的方法 注意修改以下几个地方 1.jsp页面中 ...
- DataTable 设置primarykey 后进行 Merge操作
1.先查看概念 可以看highplayer博客 http://blog.csdn.net/highplayer/article/details/6613817 2. protected void st ...
- SQL 批量插入有标识列的数据
代码: SET IDENTITY_INSERT 表名 ON SET IDENTITY_INSERT 表名 OFF
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- redis内存优化方法
先来认识2个redis配置参数 hash-max-ziplist-entries : hash内部编码压缩列表的最大值,默认512 hash-max-zipmap-value : hash内部编码压缩 ...
- Python基础入门-函数实战登录功能
''' 函数实战: .加法计算器 .过滤器 .登录功能实战 ''' def add(a,b): return a+b def login_order(): return 'asdfasdfdasfad ...