GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-javascript/18. 4Sum.js at master · chihungyu1116/leetcode-javascript

Expand file tree

/

18. 4Sum.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (41 loc) · 1.32 KB
/

18. 4Sum.js

Copy path

File metadata and controls

41 lines (41 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
var results = [];
nums.sort(function(a,b) { return a - b });
for(var i = 0; i < nums.length - 3; i++) {
while(i > 0 && i < nums.length - 3 && nums[i] === nums[i - 1]) {
i++;
}
for(var j = i + 1; j < nums.length - 2; j++) {
while(j > i + 1 && j < nums.length - 2 && nums[j] === nums[j - 1]) {
j++;
}
var low = j + 1;
var high = nums.length - 1;
var newTarget = target - (nums[i] + nums[j]);
while(low < high) {
partialSum = nums[low] + nums[high];
if(partialSum === newTarget) {
results.push([nums[i], nums[j], nums[low], nums[high]]);
high--;
low++;
while(low < high && nums[low] === nums[low - 1]) {
low++;
}
while(low < high && nums[high] === nums[high + 1]) {
high--;
}
} else if (partialSum > newTarget) {
high--;
} else {
low++;
}
}
}
}
return results;
};
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.