[LeetCode][JavaScript]3Sum

3Sum

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)

https://leetcode.com/problems/3sum/



一开始就想到暴力三重循环,提示说双指针。

先要排序,这题的解法很依赖顺序。

外层循环从头到尾,内层用双指针。

内层每一轮将两指针指向的值和外层循环的值相加,如果相等说明找到了;如果小于0,头指针往后走;大于0尾指针往前。可以这样做就是基于已经排过序了。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[][]}
 4  */
 5 var threeSum = function(nums) {
 6     var res = [];
 7     nums = nums.sort(sorting);
 8
 9     for(var i = 0; i < nums.length - 2; i++){
10         if(nums[i - 1] !== undefined && nums[i - 1] === nums[i]){
11             continue;
12         }
13         var curr = nums[i];
14         var m = i + 1;
15         var n = nums.length - 1;
16         while(m < n){
17             if(nums[m] + nums[n] + curr === 0){
18                 res.push([curr, nums[m], nums[n]]);
19                 while(m < n && nums[m] === nums[m + 1]){
20                     m++;
21                 }
22                 while(m < n && nums[n] === nums[n - 1]){
23                     n--;
24                 }
25                 m++; n--;
26             } else if(nums[m] + nums[n] + curr > 0){
27                 n--;
28             } else {
29                 m++;
30             }
31         }
32     }
33     return res;
34
35     function sorting(a, b){
36         if(a > b){
37             return 1;
38         }else if(a < b){
39             return -1;
40         }else{
41             return 0;
42         }
43     }
44 };
时间: 2024-12-25 11:40:42

[LeetCode][JavaScript]3Sum的相关文章

[LeetCode][JavaScript]3Sum Closest

3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

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

LeetCode 016 3Sum Closest

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int

[LeetCode] 015. 3Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 015.3Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和为 0. 分析: 先排序,再左右夹逼,复杂度 O(n*n).

[C++]LeetCode: 70 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 s

[Leetcode][015] 3Sum (Java)

题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过了很久,第二次做,还是不会…….好几次都是Time Limited Error.在看过正确答案之后,才知道是用的Two Pointers + sort 做的优化. 怎么优化? 简单说,就是通过 排序 + 跳过重复(利用Two Pointers) 来达到题目中避免 duplicates的要求. 核心思

LeetCode 259. 3Sum Smaller (三数之和较小值) $

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. For example, given nums = [-2, 0, 1, 3], and target = 2. Retu