求两数之和

https://leetcode.cn/problems/two-sum/

  1. function towSum(arr, target) {
  2. const map = new Map();
  3. for(let i = 0, len = arr.length; i< len; i++) {
  4. const it = arr[i];
  5. const next = target - it;
  6. if(map.has(next)) {
  7. return [map.get(next), i]
  8. } else {
  9. map.set(next, i)
  10. }
  11. }
  12. }