[LeetCode][JavaScript]4Sum

來源:互聯網
上載者:User

標籤:

4Sum 

Given an array S of n integers, are there elements a, b, c, 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.