码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]4Sum

时间:2015-07-01 06:10:20      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

4Sum 

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

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

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

 

 


 

 

我看Tags里有HashTable,想想三重循环根本用不到,就没有用这个解法。

先两两合并,再用双指针去找结果。

虽然AC了,但是写了好多好伤心,而且也速度也并不快,低于平均水平,这跟说好的不一样 (╯‵□′)╯︵┻━┻ , 肯定是使用的姿势不对。

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} target
 4  * @return {number[][]}
 5  */
 6 var fourSum = function(nums, target) {
 7     nums = nums.sort(sorting);
 8     var result = [];
 9     var countAppear = {}; twoSumMap = {}, twoSumArr = [];
10     var isFound = new Set();
11     var i, j, twoSum;
12     for(i = 0; i < nums.length; i++){
13         if(!countAppear[nums[i]]){
14             countAppear[nums[i]] = 1;
15         }else{
16             countAppear[nums[i]]++;
17         }
18         for(j = i + 1; j < nums.length; j++){
19             twoSum = nums[i] + nums[j]; 
20             if(!twoSumMap[twoSum]){
21                 twoSumArr.push(twoSum);
22                 twoSumMap[twoSum] = [{small : nums[i], large : nums[j]}];
23             }else if(!arrContains(twoSumMap[twoSum], nums[i], nums[j])){
24                 twoSumMap[twoSum].push({small : nums[i], large : nums[j]});
25             }
26         }
27     }
28     twoSumArr = twoSumArr.sort(sorting);
29     
30     var numA, numB, sum4, tmp, join;
31     for(i = 0, j = twoSumArr.length - 1; i <= j;){
32         numA = twoSumMap[twoSumArr[i]], numB = twoSumMap[twoSumArr[j]];
33         sum4 = twoSumArr[i] + twoSumArr[j];
34         if(sum4 === target){
35             addCandidate(numA, numB, result);
36         }
37         if(sum4 < target){
38             i++;
39         }else{
40             j--;
41         }
42     }
43     return result;
44     
45     function verifyResult(arr){
46         var previous = null, count = 0;
47         for(var i = 0; i < arr.length; i++){
48             if(arr[i] !== previous){
49                 previous = arr[i];
50                 count = 1;
51             }else{
52                 count++;
53                 if(count > countAppear[previous]){
54                     return false;
55                 }
56             }
57         }
58         return true;
59     }
60     function addCandidate(numA, numB, result){
61         var i, j;
62         for(i = 0; i < numA.length; i++){
63             for(j = 0; j < numB.length; j++){
64                 tmp = [numA[i].small, numA[i].large, numB[j].small, numB[j].large];
65                 tmp = tmp.sort(sorting);
66                 if(verifyResult(tmp)){
67                     join = tmp.join(‘#‘);
68                     if(!isFound.has(join)){
69                         isFound.add(join);
70                         result.push(tmp);
71                     }
72                 }            
73             }
74         }
75     }
76     function arrContains(arr, small, large){
77         for(var i = 0; i < arr.length; i++){
78             if(arr[i].small === small && arr[i].large === large){
79                 return true;
80             }
81         }
82         return false;
83     }
84     function sorting(a, b){
85         if(a > b){
86             return 1;
87         }else if(a < b){
88             return -1;
89         }else{
90             return 0;
91         }
92     }
93 };

 

 

 

 

[LeetCode][JavaScript]4Sum

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4612161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!