【数组】3Sum
题目:
Given an array S of n integers, are there elements a, b, c 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)
思路:
/**
* @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的更多相关文章
- 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 ... 
- 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 ... 
- 3Sum algorithm - 非常容易理解的实现 (java)
		原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ... 
- [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 ... 
- [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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
随机推荐
- swagger core 和 swagger ui 如何关联【窥探】
			几个片段: package io.swagger.jaxrs.listing; import io.swagger.annotations.ApiOperation; import org.apach ... 
- SSH整合 第五篇 struts2的到来
			struts2的好处,web层的显示,同时Action类相当于MVC模式的C.整合进来的话,是通过与Spring整合,减少重复代码,利用IoC和AOP. 1.struts-2.5.2.jar 以上是s ... 
- The First Android App----Adding the Action Bar
			In its most basic form, the action bar displays the title for the activity and the app icon on the l ... 
- hdu 5062 单峰数(12321)的个数
			http://acm.hdu.edu.cn/showproblem.php?pid=5062 模拟筛出对称单峰数(12321)的个数,水题 #include <cstdio> #inclu ... 
- jvm 中的 ”永生代“
			“方法区” 主要存储的信息包括:常量信息,类信息,方法信息,而且是全局共享的(多线程共享): jvm 有多种实现方式(不同的厂商): 并不是所有的jvm 都有永生代的概念: 通常情况下, 很多人把 “ ... 
- tf中softmax_cross_entropy_with_logits与sparse_softmax_cross_entropy_with_logits
			其实这两个都是计算交叉熵,只是输入数据不同. #sparse 稀疏的.稀少的 word_labels = tf.constant([2,0]) predict_logits = tf.constant ... 
- Regex Golf练习笔记(1)
			正则表达式进阶练习:https://alf.nu/RegexGolf (此练习笔记) 正则表达式验证:https://regexr.com/ (1) (2) 注释:每个词的三个字母在后面重复出现了一 ... 
- C#HttpUtility.UrlEncode 大写问题
			工作上和另一个公司对接,调对方的api需要用到md5加密,加密前要使用HttpUtility.UrlEncode,对方接口一直返回验证错误,定位了问题发现是中文编码使用HttpUtility.UrlE ... 
- Newtonsoft.Json日常用法
			原文链接:https://www.cnblogs.com/ZengJiaLin/p/9578794.html 
- Django 一些少用却很实用的orm查询方法
			一.使用Q对象进行限制条件之间 "或" 连接查询 from django.db.models import Q from django.contrib.auth.models im ... 
