-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (44 loc) · 1.09 KB
/
app.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
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
let temp = nums.slice();
let sorted = nums.sort((num1, num2) => {
return num1 - num2;
});
let firstIndex = 0;
let lastIndex = sorted.length - 1;
while (firstIndex < lastIndex) {
let sum = sorted[firstIndex] + sorted[lastIndex];
if (sum > target) {
lastIndex--;
} else if (sum < target) {
firstIndex++;
} else if (sum === target) {
break;
}
}
let num1 = sorted[firstIndex];
let num2 = sorted[lastIndex];
console.log("unsorted", temp);
console.log("---------------------");
console.log("sorted", sorted);
console.log("indices", firstIndex, lastIndex);
console.log("numbers", num1, num2);
let result = [];
let idx1 = temp.indexOf(num1);
let idx2;
if (num1 === num2) {
idx2 = temp.indexOf(num2, idx1 + 1);
} else {
idx2 = temp.indexOf(num2);
}
console.log("unsroeted indices", idx1, idx2);
return [idx1, idx2];
};
let nums = [2, 7, 11, 15];
twoSum(nums, 9);
let nums1 = [3, 2, 4];
twoSum(nums1, 6);