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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
随机推荐
- MySQL5.7 在CentOS 下的安装
尝试了在版本的CentOS6.6 和CentOS7.2 下安装,在6.6下比较复杂些.特地做下记录 在CentOS7.2 下安装,需要在官网下载 mysql-5.7.16-1.el7.x86_64.r ...
- 【HDU6024】Building Shops
题意 有n个教室排成一排,每个教室都有一个坐标,现在,小Q想建一些糖果商店,在这n个教室里面.总的花费有两部分,在教室i建一个糖果屋需要花费ci,对于没有任何糖果屋的P,需要的花费为这个教室到它左边有 ...
- ZOJ 3956 Course Selection System
题意 有n节课可供选择,每节课都有两个值Hi和Ci,如果学生选择了m节课(x1,x2,....,xm),则它的舒适值被定义为: //这里没有公式((lll¬ω¬)),因为那个图片我保存不下来≧ ﹏ ≦ ...
- 算法描述》LCA两三事(蒟蒻向)
LCA是图论中常用的解决树形结构子问题的工具,这一问题一般需要用一个简短的子函数直接解决,但是这对于广大蒟蒻们仍然是一个不小的问题. LCA是指在树形结构中两点的最近公共祖先,对于这个问题,直接向上找 ...
- Codeforces 1109E 线段树
思路及博客:https://www.cnblogs.com/uid001/p/10507346.html 代码: #include <bits/stdc++.h> #define LL l ...
- Tensorflow CPU mask-rcnn 训练模型
基于cpu版的tensorflow ,使用mask_rcnn训练识别箱子的模型 代码参考(https://blog.csdn.net/disiwei1012/article/details/79928 ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- 1.ef 映射关系
1.edmx <?xml version="1.0" encoding="utf-8"?><edmx:Edmx Version="3 ...
- markdown的图片外链
markdown的图片用本地的很不方便,今天试用了一下七牛的服务,感觉很好用.推荐一下,免费的服务够用并且比较友好.
- linux学习1----初涉linux
linux因其稳定高效的特点,受到很多开发者的青睐,因此将其作为服务器的操作系统. 作为一名开发者,程序员,掌握了一定的linux知识和技巧,程序的开发部署和运行也有不小的帮助. linux由于其开源 ...