-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtosum.js
More file actions
61 lines (46 loc) · 1.56 KB
/
tosum.js
File metadata and controls
61 lines (46 loc) · 1.56 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
61
// function getTwoSum(array,targetSum){
// for (let firstIndex = 0; firstIndex < array.length - 1; firstIndex++) {
// const firstNumber = array[firstIndex];
// for (let secondIndex = firstIndex + 1; secondIndex < array.length; secondIndex++) {
// const secondNumber = array[secondIndex];
// if (firstNumber + secondNumber === targetSum) {
// return [firstNumber,secondNumber]
// }
// }
// }
// return []
// }
// let result = getTwoSum([14,36,89,8],10)
// console.log(result);
// function getTwoSumV2(array,targetSum){
// let leftSide = 0;
// let rightSide = array.length - 1
// while (leftSide < rightSide) {
// let firstNumber = array[leftSide]
// let secondNumber = array[rightSide]
// let sum = firstNumber + secondNumber
// if (sum > targetSum) {
// rightSide --
// }else if(targetSum > sum){
// leftSide ++;
// }else{
// return [firstNumber,secondNumber]
// }
// }
// }
// let result = getTwoSumV2([1,2,3,4],4)
// console.log(result);
// function getTwoSumV3(array,targetSum){
// const map = {}
// for (let index = 0; index < array.length; index++) {
// const firstNumber = array[index];
// const othersNumber = targetSum - firstNumber
// if (map[othersNumber]) {
// return [firstNumber,othersNumber]
// }
// map[othersNumber] = true
// }
// return []
// }
// let result = getTwoSumV3([1,2,3,4],6)
// console.log(result);