Topic
Given an array of integers, return indices of the both numbers such that they add-to a specific target.
You may assume this each input would has exactly one solution, and you could not use the same element Twi Ce.
Example:
Given nums = [2, 7, one, 2], target = 9,because nums[0] + nums[1] = + 7 = 9,return [0, 1].
Code
/** * @param {number[]} Nums * @param {number} target * @return {number[]} *///[2, 7, 11, 15] 9//Put the difference into an array if the current traversal array has a value equal to the difference The subscript of the current value and the subscript of the difference are output to var twosum = function (Nums, target) {let Theset = [] for (Let i = 0; i < nums.length; i++) { if (Theset.indexof (Nums[i])!==-1) {//Current number and array of pairs exists return [Theset.indexof (Nums[i]), I]; Returns the current value in the array, if any, and the current subscript}else{Theset.push (target-nums[i]);//If the array has no current value subtract the difference in the array 9-2=7 i=1--9-7=2 i=2- -9-11=-2 i=3--9-15=-6 i=4}} return [0,0];}; /*var twosum = function (Nums, target) {var arr = []; var num = []; /* for (var i = 0;i<nums.length;i++) {for (var j = i+1; i< nums.length; j + +) {if (nums[i] = = Targ Et-nums[j]) {arr = [i,j]; return arr; }}} ***};var Twosum = function (Nums, target) {const DIFFS = new Map (); Const J = Nums.findindex ((a, I) = Diffs.has (target-a) | | Diffs.set (A, i)&& 0); return [Diffs.get (Target-nums[j]), j];}; */
Algorithm: The sum of JavaScript two