1. JavaScript LeetCode 1: the Sum of Two Numbers | Two Sum

LeetCode first question source: LeetCode link: leetcode-cn.com/problems/tw…

Topic describes

Given an array of integers nums and an integer target value target, find the two integers in the array that and are the target values and return their array subscripts. You can assume that there is only one answer for each type of input. However, the same element in the array cannot be repeated in the answer. You can return the answers in any order.

Example 1:
Input: nums = [2,7,11,15], target = 9 output: [0,1]Copy the code
Example 2:
Input: nums = [3,2,4], target = 6Copy the code
Example 3:
Input: nums = [3,3], target = 6Copy the code

Problem solving, ideas

The titled LeetCode first topic, also is in the front end of the interview questions, higher frequency is, the problem is I have two kinds of thinking, a kind of time complexity to pursue and a space complexity to pursue and generally see the problem, think of is the first time cycle twice, do low space complexity but high time complexity, Sometimes an interview will run into a time limit because it will run faster.

I’m going to loop it twice. Time order n ^ 2.

There’s no explanation for the idea of doing it twice, just doing it twice, and then deciding if the sum of the first and second times equals the target.

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  for (var i = 0; i < nums.length; i++) {
    for (var j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j]
      }
    }
  }
};
Copy the code

Loop it over using the time complexity O(n) of the Map object

It is more convenient to use the Map Object to save key-value pairs, together with the Map Object’s set, GET, and HAS methods. You can also choose to use Object. The idea is to determine whether there is a difference between the target value and the current value in the Map Object during the loop. If not, the Map object is Set with the current value as key and the subscript as value

/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, Target) {// Create a Map object const m = new Map(); for(let i = 0; i < nums.length; If (m.has(target-nums [I])){return [m.get(target-nums [I]), Int (nums[I], I)}};Copy the code

Loop through using Object to simulate Map time complexity O(n)

The idea of using objects is much the same as that of using Maps, except that objects do not have as many methods as Maps. The loop determines whether the object has a key that is the difference between the target value and the current value. If so, the key is returned. If not, the key is saved to the object

/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, Let obj = {} for (let I = 0; i < nums.length; If (obj[target-nums [I]]! If (obj[target-nums [I]]! Return obj[target - nums[I]], I]} obj[nums[I]] = I};Copy the code

It occurred to me that we could also use the indexOf method to do this with the Map object, but this would increase the time complexity, but should be able to achieve the minimum number of rows. It has been three months since I posted my last article. I am speechless to myself. I have no self-discipline, no precipitation and accumulation, and words have not persisted. When will I be able to drive myself!!