Skip to content

Commit c102fc8

Browse files
committed
two sum solution
1 parent 29c9a3b commit c102fc8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

two-sum/Yg-cho.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
for(var i = 0; i < nums.length; i++){
8+
let getNum = target - nums[i];
9+
let foundIndex = nums.indexOf(getNum);
10+
11+
// foundIndex가 존재하고(-1이 아니고), 자기 자신이 아닌 경우
12+
if(foundIndex !== -1 && foundIndex !== i) {
13+
return [i, foundIndex];
14+
}
15+
}
16+
};
17+
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1]
18+
console.log(twoSum([3, 2, 4], 6)); // [1, 2]
19+
console.log(twoSum([3, 3], 6)); // [0, 1]

0 commit comments

Comments
 (0)