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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
随机推荐
- Spring项目的发展历史和SpringBoot的发展历史
Spring项目的发展历史和SpringBoot的发展历史 在Java做web应用的服务端开发领域,一直存在着两套技术体系,一套是Sun公司官方推出的JavaEE,另一套是Spring.Spring ...
- sendCloud群发邮件一点总结
1.群发时,若发送的邮件为html页面,则不能用[普通发送]然后foreach循环: 若是单纯的文本,则可以用普通发送,否则,第一封邮件成功后,后面的都是html乱码. 2.若要用html模板发送,可 ...
- Scala介绍
强大的编程语言 Scala 是一门非常强大的语言,它允许用户使用命令和函数范式进行编写代码,因此,编程时你可以使用常用的命令式语句,就像我们使用 C.Java.PHP 以及很多其他语言一样,而且,你也 ...
- 2014蓝桥杯B组初赛试题《李白打酒》
题目描述: 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上 ...
- Win10系统优化/设置脚本
Win10系统优化/设置脚本 用了很长时间win10了,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win10的系统设置,结合DWS对Win1 ...
- 安装CentOS 6网络配置问题
安装CentOS 6网络配置问题 今天决定把家中的CentOS从5升级至6.但安装完CentOS 6.2之后发现eth0没有像往常一样通过DHCP自动获取IP.打开“/etc/sysconfig/ne ...
- 为什么要有http响应码
其实还是比较容易理解的.这就和你去小卖部买东西一样,老板,我想要一袋大米,那老板先得回答有还是没有,还是说我们这没有,去其它地方买去吧,得先给个说法,这个说法就是http相应码,有了http响应码之后 ...
- python3--装饰器高级学习版
__author__ = "Aaron Fan"import time #导入time模块user,passwd = 'alex','abc123' #用户名密码def auth( ...
- (转)使用Jquery+EasyUI 进行框架项目开发案例讲解之四---组织机构管理源码分享
原文地址:http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码 ...
- wcf文件上传时碰到的配置问题
1.远程服务器返回了意外相应:(413) Request Entity Too Large 修改客户端配置maxReceivedMessageSize="2147483647" & ...