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]
]

题意:

给定一个数组和一个值target,找到所有三数加起来等于target的组合

Solution1:Two Pointers(left and right to meet each other)

1. Sort the array (coz we must check the duplicates)

2. Lock one pointer and do two sum with the other two

Step1: Sort the given array in ascending order

Step2: Lock pointer i, then do two sum with two pointers j, k

Step3:  checking nums[i] + nums[j] + nums[k] == target ?

if   nums[i] + nums[j] + nums[k] < target,  pointer j ++

if   nums[i] + nums[j] + nums[k] > target,  pointer k --

注意几点:

1、题目要求“The solution set must not contain duplicate triplets.” 每次移动 i , j , k 都注意查重

2、Arrays工具类中常用方法需要牢记:

Arrays.sort() 排序数组

Arrays.fill() 填充数组

Arrays.toString() 将int数组转成string数组

Arrays.asList() 将数组转成list集合

code:

 /*
Time Complexity: O(n^2). For each locked item, we need traverse the rest of array behind such item.
Space Complexity: O(1). We only used constant extra space.
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int target = 0;
Arrays.sort(nums);
// corner case
if (nums.length < 3) return result; for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicates
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] < target) {
j++;
while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
} else if (nums[i] + nums[j] + nums[k] > target) {
k--;
while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
j++;
k--;
while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
}
}
}
return result;
}
}


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

  1. [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 ...

  2. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  3. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  4. 【LeetCode 15】三数之和

    题目链接 [题解] 先把n个数字升序排个序. 然后枚举三元组最左边的那个数字是第i个数字. 之后用两个指针l,r移动来获取三元组的第2个和第3个数字. (初始值,l=i+1,r = n-1); 如果a ...

  5. [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 ...

  6. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  7. LeetCode 第15题-三数之和

    1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...

  8. LeeCode数组第15题三数之和

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

  9. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

随机推荐

  1. MyTomcat(手写服务器)

    Tomcat 是非常流行的 Web Server,它还是一个满足 Servlet 规范的容器.那么想一想,Tomcat 和我们的 Web 应用是什么关系? 从感性上来说,我们一般需要把 Web 应用打 ...

  2. JS 60秒后重发送验证码

    //settime($("#getPhoneCode"),60); function settime($obj, time) { if (time == 0) { $obj.att ...

  3. .Net MVC TextBoxFor 扩展 placeholder 与 class 属性

    namespace System.Web.Mvc { public static class HtmlHelperExtensions { public static MvcHtmlString Bs ...

  4. problem:浏览器如何区分html超文本和普通文本

    运营同学问:后端返回的一串元素标签,我想在网页中显示的时候,将标签中的内容渲染出来,不希望直接显示标签. 回答:bootstrap加模版组织的网页,模版渲染的数据只能渲染字符串,不能转化富文本. 运营 ...

  5. 数据恢复工具--extundelete的安装与使用

    1.extundelete的恢复原理 extundelete恢复文件时并不依赖特定文件格式,首先extundelete会通过文件系统的inode信息,来获得当前文件系统下所有文件的信息,包括存在的和已 ...

  6. Problem C: 重复子串(string)

    /* 一个性质? right集合中只有相邻的位置才会有用 那么考虑set启发式合并, 能够求出大概nlogn个有用的对 那么将这些对按照右端点排序, 查询也按照右端点排序就可以离线维护信息 然后需要维 ...

  7. 在BootStrap的modal中使用Select2搜索框无法输入

    用modal来show一个对话框 dialog.modal({ backdrop:true, keyboard:true, show:true }); 1 2 3 4 5 然后再modal中初始化se ...

  8. Java date 日期计算

    import org.junit.Test; import java.util.Calendar; import java.util.Date; /** * @author cosmo * @Titl ...

  9. leetcode每日刷题计划-简单篇day1

    orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...

  10. 一种storyboard+swift实现页面跳转的方法

    如题.视图控制器A显示视频列表:视图控制器B显示视频详情,现希望将两个视图关联起来,点击A中某个视频跳转到B. 作为iOS小菜鸟我首先搜索了一下关键词 “tableviewcell 跳转”,然而搜索结 ...