-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_3Sum_skip_duplicates.js
68 lines (60 loc) · 2.01 KB
/
15_3Sum_skip_duplicates.js
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
61
62
63
64
65
66
67
68
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
let l_index = 0
let ans = []
nums.sort((a, b) => a - b) // O(n log n)
// console.log(nums)
// find triplets
while (l_index < nums.length-2) {
let m_index = l_index+1
while (m_index < nums.length-1) {
let r_index = m_index+1
while(r_index < nums.length) {
// console.log(l_index, m_index, r_index)
if (nums[l_index] + nums[m_index] + nums[r_index] == 0) {
let temp_ans = [nums[l_index], nums[m_index], nums[r_index]]
if (ans.length == 0) {
ans.push(temp_ans)
} else {
// check if there is duplicate
let hasDuplicate = false
for (let i=0; i<ans.length; i++) {
if (isArrayEqual(ans[i], temp_ans)) {
hasDuplicate = true
break
}
}
if (!hasDuplicate) {
ans.push(temp_ans)
// console.log('answer added: ' + temp_ans)
} else {
// console.log('answer not added: ' + temp_ans)
}
}
}
r_index = findNextIndex(r_index)
}
m_index = findNextIndex(m_index)
}
l_index = findNextIndex(l_index)
}
return ans
function isArrayEqual(array1, array2) {
for (let i=0; i<array1.length; i++) {
if (array1[i] != array2[i]) {
return false
}
}
return true
}
function findNextIndex(index) {
let new_index = index + 1
while (nums[index] == nums[new_index]) {
new_index += 1
}
return new_index
}
};