2019-05-14-TwoSum

1309 단어
  • 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;
      }
    };
    

    좋은 웹페이지 즐겨찾기