题目:

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. 用Git将项目发布在GitHub里

    转载自http://blog.csdn.net/u011572517/article/details/50537407,个人加了一些注意事项和解释. githud是一个程序员以后成长都会使用到的,先不 ...

  2. passwd: Have exhausted maximum number of retries for service

    使用命令passwd修改密码时,遇到如下问题:# echo 'utf8'|passwd zhangsan --stdinChanging password for user zhangsan.pass ...

  3. 判断Mouse事件源类型

    //鼠标事件源类型 public enum MouseEventSource { Mouse, Pen, Touch } /// <summary> /// MainWindow.xaml ...

  4. ESP32应用程序的内存布局

    应用程序内存布局 ESP32芯片具有灵活的内存映射功能.本节介绍ESP-IDF在默认情况下如何使用这些功能. ESP-IDF中的应用程序代码可以放置在以下内存区域之一中. IRAM(指令RAM) ES ...

  5. 初探FFT(快速傅里叶变换)

    第一次接触省选的知识点呢!zrf大佬在课堂上讲的非常清楚,但由于本蒟蒻实在太菜了,直接掉线了.今天赶紧恶补一下. 那么这篇博客将分为两块,第一块是FFT的推导和实现,第二块则是FFT在OI上的应用 因 ...

  6. 【算法34】蓄水池抽样算法 (Reservoir Sampling Algorithm)

    蓄水池抽样算法简介 蓄水池抽样算法随机算法的一种,用来从 N 个样本中随机选择 K 个样本,其中 N 非常大(以至于 N 个样本不能同时放入内存)或者 N 是一个未知数.其时间复杂度为 O(N),包含 ...

  7. asp.net缓存使用介绍

    介绍: 在我解释cache管理机制时,首先让我阐明下一个观念:IE下面的数据管理.每个人都会用不同的方法去解决如何在IE在管理数据.有的会提到用状态管理,有的提到的cache管理,这里我比较喜欢cac ...

  8. C#在dataGridView中遍历,寻找相同的数据并定位

      1. C#在dataGridView中遍历,寻找相同的数据并定位   [c-sharp] view plain copy int row = dataGridView1.Rows.Count;// ...

  9. NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json

    一.简介 1.读取配置文件是开发过程中使用非常频繁的操作.属称”不能写死“ 二.NetCore读取配置文件 1.新建一个静态公共变量,属称单例. 2.在程序Startup启动时,将系统变量传递给单例. ...

  10. Spring Boot - StateMachine状态机

    是Spring Boot提供的状态机的现成实现. 理论(有点像工作流) 需要定义一些状态的枚举,以及一些引起状态变化的事件的枚举. 每个状态可以对应的创建一个继承自org.springframewor ...