题目:

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
(-1, 0, 1)
(-1, -1, 2)

思路:

设置两个指针,一个指头,一个指尾,用binary search找需要的元素。找到就加上,没有就去下一步。
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
var n=nums.length,temp=0,res=[];
if(nums.length<3){
return [];
}
nums.sort(function(a,b){return a-b;});
for(var i=0;i<n-2;i++){
if(nums[i]>0){
break;
}
for(var j=n-1;j>i+1;j--){
temp=-(nums[i]+nums[j]);
var l=i+1,r=j-1;
if(nums[l]>temp||nums[r]<temp){
break;
}
while(l<=r){
var mid=l+Math.floor((r-l)/2);
if(nums[mid]==temp){
var arr=[nums[i],nums[mid],nums[j]];
res[res.length]=arr;
break;
}else if(nums[mid]<temp){
l=mid+1;
}else{
r=mid-1;
}
}
}
} return res;
};

【数组】3Sum的更多相关文章

  1. 3sum(从数组中找出三个数的和为0)

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

  3. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  4. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  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:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  7. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. No.016:3Sum Closest

    问题: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. No.015:3Sum

    问题: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?Find all ...

随机推荐

  1. N个数的最大公约数

    #include <iostream> using namespace std; int main() { int c; ]={,,,}; ;i<;i++) { ]<m[i]) ...

  2. 201709019工作日记--sleep、wait、notify的使用详解

    1. sleep()和wait()的区分 (1)这两个方法来自不同的类分别是,sleep来自Thread类,wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在 ...

  3. 使用Octopress博客 搭建博客

    Octopress介绍 Octopress是一款优秀的静态化博客系统,也是一个本地化的博客系统,之前部落在介绍免费开源Github Pages空间时有提到过Octopress,Github为我们提供长 ...

  4. 基于MATLAB的均值滤波算法实现

    在图像采集和生成中会不可避免的引入噪声,图像噪声是指存在于图像数据中的不必要的或多余的干扰信息,这对我们对图像信息的提取造成干扰,所以要进行去噪声处理,常见的去除噪声的方法有均值滤波.中值滤波.高斯滤 ...

  5. bootstrap modal

    模态框提供了两个可选尺寸,通过为 .modal-dialog 增加一个样式调整类实现.加modal-lg,加modal-sm,不加也可以,共有三种尺寸. 触发方式,data-target, 感觉比js ...

  6. Fading Like a Flower

    Fading Like a Flower In a time where the sun descends alone 伴着落日孤独的脚步 I ran a long long way from hom ...

  7. [label][politic-video]李锡锟的政治学视频下载链接

    李锡锟政治学 1.http://r15---sn-p5qlsn7y.googlevideo.com/videoplayback?initcwndbps=1471000&signature=09 ...

  8. Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题

    //php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_av ...

  9. session token防表单重提

    1.表单页面初始化前,先在session存入一个token值,随后把token存放在表单页面隐藏表单域内,开始初始化: 在表单页初始化前,调用ajax请求,在后台生成token,并返回至表单页 fun ...

  10. Android-AndroidManifest.xml默认启动的Activity(探索篇01)

    AndroidManifest.xml-->默认启动  MusicBrowserActivity <activity android:name=".MusicBrowserAct ...