into-piece / Step-By-Step

每天一题向前端架构师前进
4 stars 1 forks source link

两数之和 #8

Open into-piece opened 5 years ago

into-piece commented 5 years ago

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。


const getAddArray = (arr, target) =>
arr.reduce((res, cur, i) => {
return arr.includes(target - cur)
? [...new Set([...res, cur, target - cur])]
: res;
}, []);

let array = [1, 2, 3, 4, 0]; console.log(getAddArray(array, 3));