-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.三数之和.js
More file actions
61 lines (59 loc) · 1.69 KB
/
Copy path15.三数之和.js
File metadata and controls
61 lines (59 loc) · 1.69 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* @lc app=leetcode.cn id=15 lang=javascript
*
* [15] 三数之和
*
* https://leetcode-cn.com/problems/3sum/description/
*
* algorithms
* Medium (21.56%)
* Total Accepted: 46K
* Total Submissions: 211.8K
* Testcase Example: '[-1,0,1,2,-1,-4]'
*
* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0
* ?找出所有满足条件且不重复的三元组。
*
* 注意:答案中不可以包含重复的三元组。
*
* 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
*
* 满足要求的三元组集合为:
* [
* [-1, 0, 1],
* [-1, -1, 2]
* ]
*
*
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const resultNumArr = [];
nums = nums.sort((a, b) => { return (a - b) })
for (let i = 0; i < nums.length - 2; i++) {
if (i == 0 || (i > 0 && nums[i] !== nums[i - 1])) {
const sum = 0 - nums[i]
let j = i + 1;
let x = nums.length - 1;
while (j < x) {
if (nums[x] + nums[j] == sum) {
resultNumArr.push([nums[i], nums[j], nums[x]])
while (j < x && nums[j] === nums[j + 1]) { j++ }
while (j < x && nums[x] === nums[x - 1]) { x-- }
j++;
x--;
} else if (nums[x] + nums[j] < sum) {
while (j < x && nums[j] === nums[j + 1]) { j++ }
j++;
} else {
while (j < x && nums[x] === nums[x - 1]) { x-- }
x--;
}
}
}
}
return resultNumArr;
};