### Title

``给定 nums = [2, 7, 11, 15], target = 9``

### Analyze

``````var twoSum = function(nums, target) {
var length = nums.length
for (var x = 0; x < length; x++) {
for (var y = x + 1; y < length; y++) {
if (nums[x] + nums[y] === target) {
var arr = []
arr.push(x)
arr.push(y)
return arr
}
}
}
}``````
• 时间复杂度: O(n^2)
• 空间复杂度: O(1)

• 遍历一次数组, 查询当前`哈希表`中是否有和当前索引值 `nums[i]` 对应的匹配值 `target - nums[i]`;
• 若有, 则返回它们两个值的索引;
• 若没有, 则将当前索引值和下标存入哈希表中;
``````var twoSum = function(nums, target) {
let numsObj = {}
for (let i = 0; i < nums.length; i++) {
let current = nums[i]
let match = target - current
if (match in numsObj) {
return [i, numsObj[match]]
}
numsObj[current] = i
}
}``````
• 时间复杂度: O(n)
• 空间复杂度: O(n)

``````var twoSum = function(nums, target) {
var map = new Map()

for (let i = 0; i < nums.length; i++) {
const targetValue = target - nums[i]
const getTargetValue = map.get(targetValue)
if (typeof(getTargetValue) === 'number') {
return [i, getTargetValue]
}
map.set(nums[i], i)
}
}``````
• 时间复杂度: O(n)
• 空间复杂度: O(n)

15、16、18