2019-05-14-TwoSum

  • two sum
    From 21:10 - 21:30

  • array

  • map

My solution:

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    var res = [];
    
    // value : index
    var map = {};
    
    // tranfer arr to map
    // attrieve complexity n -> 1
    for(var i=0; i
  • Other submittion
var twoSum = function(nums, target) {
    for(let i=0;i
var twoSum = function(nums, target) {
  const map = {};
  
  for(let i = 0; i < nums.length; i++) {
    const currentValue = nums[i];
    const wantedValue = target - currentValue;
    if(map[wantedValue] !== undefined) {
      return [map[wantedValue], i];
    }
    map[currentValue] = i;
  }
};

你可能感兴趣的:(2019-05-14-TwoSum)