求 nums[0] + nums[ n ] === target 的
用 Map
var twoSum = function(nums, target) {
let map = new Map()
for(let i =0;i < nums.length; i++){
let x = target-nums[i]
if(map.has(x)){
return [map.get(x),i]
}
map.set(nums[i],i)
}
};
- 暴力解
var twoSum = function(nums, target) {
const len = nums.length;
for (let i = 0; i < len - 1; i++) {
for (let j = i + 1; j < len; j++) {
if (nums[i] + nums[j] === target) {
return [i, j]
}
}
}
}