-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1 Two Sum.js
More file actions
35 lines (31 loc) · 976 Bytes
/
1 Two Sum.js
File metadata and controls
35 lines (31 loc) · 976 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
26
27
28
29
30
31
32
33
34
35
/**
* 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.
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数。
* 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
*/
/**
* Example:
* Given nums = [2, 7, 11, 15], target = 9,
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1].
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = (nums, target) => {
let res = []
nums.forEach((num, index) => {
for (let i = index + 1; i < nums.length; i++) {
if (num + nums[i] === target) {
res.push(index, i)
}
}
})
return [...new Set(res)]
}
console.log(twoSum([2, 7, 11, 15], 9))