forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
25 lines (24 loc) · 617 Bytes
/
res.js
File metadata and controls
25 lines (24 loc) · 617 Bytes
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
/**
* Problem: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
*
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
*
* two-sum.js
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
let twoSum = function(nums, target) {
for (let i = nums.length - 1; i >= 0; i--) {
for (let j = 0; j < i; j++) {
if ( addition(nums[i], nums[j]) === target ) {
return [j, i];
}
}
}
};
// add a with b and return it
let addition = function(a, b) {
return a + b;
};