Skip to content

Commit dabb24d

Browse files
author
bhan
committed
two sum solution
1 parent 03140c1 commit dabb24d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

two-sum/byol-han.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
2+
3+
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
5+
// You can return the answer in any order.
6+
7+
/**
8+
* @param {number[]} nums
9+
* @param {number} target
10+
* @return {number[]}
11+
*/
12+
var twoSum = function (nums, target) {
13+
let result = [];
14+
for (let i = 0; i < nums.length; i++) {
15+
for (let j = i + 1; j < nums.length; j++) {
16+
if (nums[i] + nums[j] === target) {
17+
return [i, j];
18+
}
19+
}
20+
}
21+
return result;
22+
};

0 commit comments

Comments
 (0)