-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwosum.js
More file actions
51 lines (46 loc) · 1.17 KB
/
twosum.js
File metadata and controls
51 lines (46 loc) · 1.17 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
// Two Sum
// Example 1:
// Input: nums = [2,7,11,15], target = 9
// Output: [0,1]
// Example 2:
// Input: nums = [3,2,4], target = 6
// Output: [1,2]
// Example 3:
// Input: nums = [3,3], target = 6
// Output: [0,1]
var twoSum = function (nums, target) {
let st = null;
let ed = null;
for (let i = 0; i < nums.length; i++) {
let start = nums[i]; // starting with first in array
// add other and check if same as target
let cpy = Array.from(nums);
cpy.splice(i, 1);
for (let j = 0; j < cpy.length; j++) {
let sum = cpy[j] + start;
if (sum == target) {
st = cpy[j];
ed = start;
}
}
}
let stIdx = nums.indexOf(st);
let edIdx = nums.lastIndexOf(ed);
return [stIdx, edIdx];
};
// method 2 Note: Array must be sorted or this will not work
var twoSum = function (numbers, target) {
let left = 0;
let right = numbers.length - 1;
while (left < right) {
let sum = numbers[left] + numbers[right];
if (sum == target) {
return [left + 1, right + 1]
} else if (sum < target) {
left += 1;
} else if (sum > target) {
right -= 1;
}
}
return []
};