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. WPF中获取控件默认样式和模板XML

    从微软官方找这个东西甚是困难,似乎根本没有提供.网上说因为版本问题,很难找到,但通过代码却可以轻易获得.经测试,生成的样式文件非常完美,完全不用修改即可应用. 代码如下: public static ...

  2. Django请求流程图

    Django请求流程图

  3. Ajax(Asynchronous JavaScript )and xml

    JavaScript的两种任务执行模式--同步(synchronous)和异步(Asynchronous) 同步模式 JavaScript的执行环境是单线程的,意味着一次只能执行一个任务,如果有多个任 ...

  4. ubuntu18.04安装openresty

    ubuntu18.04使用openresty官方APT源安装openresty 添加openresty的 APT 仓库,这样就可以便于未来安装或更新软件包(通过 apt-get update 命令). ...

  5. py-day3-1 python 风湿理论之函数即变量

    # 风湿理论之函数即变量 def foo(): print('from foo') bar() def bar(): print('from bar') foo() from foo from bar ...

  6. js EL 正则表达式

    <script> //校验是否全由数字组成20位数 var patrn=/^[0-9]{1,20}$/; alert(patrn.test("-30000000000" ...

  7. centos下vi的用法大全

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  8. git push 和 pull 时 免密执行的方法

    问题:在使用git代码仓库时,使用git clone 获取代码时,如果使用的是https协议,则在每次push时需要输入账号密码.相关文档:文档一,文档二 验证了文档一种的方法二可用,记录一下创建文件 ...

  9. 二叉树遍历(flist)(已知中序和按层遍历,求先序 )

    问题 F: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...

  10. Vue 父组件调用子组件函数的方法

    parent.vue(父组件的内容): <template> <div @click="divClick"> <info-wnd ref=" ...