【题目】

Given an array nums of n integers, are there elements abc 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]
]

【思路】

拓展四数之和:https://www.cnblogs.com/inku/p/9977917.html

sort后,头尾两个指针。

sum=0,left++,right--,继续遍历,向中间逼近。

sum>0,right--,需要更小的数使之满足。

sum<0,left++,需要更大的数使之满足。

 if(sum==0){
data.add(Arrays.asList(nums[i],nums[left], nums[right]));
left++;
right--;
while(left<right&&nums[left]==nums[left-1])
left++;
while(left<right&&nums[right]==nums[right+1])
right--;
}
else if(left<right&&sum>0)
right--;
else
left++;
}

答案去重

if(i>0&&nums[i]==nums[i-1]) continue;
while(left<right&&nums[left]==nums[left-1]) left++;
while(left<right&&nums[right]==nums[right+1]) right--;

【代码】

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> data=new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
int left=i+1;
int right=nums.length-1;
if(i>0&&nums[i]==nums[i-1]){
continue;} while(left<right){
int sum=nums[left]+nums[right]+nums[i];
if(sum==0){
data.add(Arrays.asList(nums[i],nums[left], nums[right]));
left++;
right--;
while(left<right&&nums[left]==nums[left-1])
left++;
while(left<right&&nums[right]==nums[right+1])
right--;
}
else if(left<right&&sum>0)
right--;
else
left++;
}
}
return data;
}
}

[Leetcode 15]三数之和 3 Sum的更多相关文章

  1. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  2. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  3. LeetCode——15. 三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  4. LeetCode 15. 三数之和(3Sum)

    题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  5. [LeetCode]15. 三数之和(数组)(双指针)

    题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三 ...

  6. [LeetCode] 15. 三数之和

    题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...

  7. LeetCode:三数之和【15】

    LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...

  8. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  9. [Leetcode 18]四数之和 4 Sum

    [题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...

随机推荐

  1. How do you explain Machine Learning and Data Mining to non Computer Science people?

    How do you explain Machine Learning and Data Mining to non Computer Science people?   Pararth Shah, ...

  2. SQL Server 百万级数据提高查询速度的方法(转)

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描.2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及 ...

  3. CSS实现经典的三栏布局

    实现效果: 左右栏定宽,中间栏自适应 (有时候是固定高度) 1 . 绝对定位布局:position + margin <div class="container"> & ...

  4. servlet injection analysis

    一. Spring不能通过注解向Servlet中注入实例的原理 想了解此问题的原理,就要了解tomcat启动后 servlet和spring的加载顺讯. 1.  tomcat启动后先加载web.xml ...

  5. php实现微信网页授权回调代理

    一个简单的php文件,实现微信网页授权回调域名的代理转发  <?php function is_HTTPS() { if (!isset($_SERVER['HTTPS'])) return F ...

  6. 从路由器镜像中提取uImage头信息

    uImage header为64字节,文件头为27 05 19 56 hexdump -C a.bin | grep "27 05 19 56" 或者 hd aa.bin | gr ...

  7. PHP实现简单发红包(随机分配,平均分配)

    最近碰到一些情况,把思路重新整理了一下,敲出代码.记下来,以后可以借鉴,进一步优化等. 大致的思路:红包主要分两种,一种是平均分配,一种是随机分配. 1.平均分配 平均分配相对好理解,只要把钱平均分给 ...

  8. python阶段性总结

    一,学习方法 说起来我也是第一次学习python,一开始也是什么都不懂.当开始学习一个新的知识时,我觉得第一件事便是了解它的基本概念.一定要认认真真的阅读参考书至少一次,用笔勾画出你所认为的重点和难点 ...

  9. eclipse软件仿真操作

    1.编写程序代码(以SDRAM为例) 1.1 编写head.s汇编文件 .equ SDRAM_BASE, 0x30000000 .equ MEM_CTL_BASE, 0x48000000 .text ...

  10. openwrt为何需要refresh新增的补丁?

    答:为了避免应用新补丁时出现无法应用的问题 如普通package的补丁refresh: make package/example/refresh V=s 如kernel的补丁refresh: make ...