This is the 23rd day of my participation in Gwen Challenge

  • Here is a collection of various javascript solutions to the sum of two numbers

The title

Given an integer array nums and an integer target value target, find the two integers in the array and the target value target 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:

Enter: nums = [2.7.11.15], target = 9Output:0.1Because nums[0] + nums[1] = =9To return to the [0.1].Copy the code
  • Example 2:
Enter: nums = [3.2.4], target = 6Output:1.2]
Copy the code
  • Example 3:
Enter: nums = [3.3], target = 6Output:0.1]
Copy the code

solution

/ * * *@param {number[]} nums
 * @param {number} target
 * @return {number[]}Violence enumeration inside and outside double cycle, simple, easy to understand. * Time complexity: O(N^2), where N is the number of elements in the array * space complexity: O(1) */

/** 92ms */
var twoSum = function (nums, target) {
    for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] === target) {
                return[i, j]; }}}};Copy the code
/ * * *@param {number[]} nums
 * @param {number} target
 * @return {number[]}* Time complexity: O(N), where N is the number of elements in the array * space complexity: O(N), where N is the number of elements in the array */
/** 80ms */
var twoSum2 = function(nums, target) {
   Static hash table method
   const map = new Map(a);for(let i = 0; i < nums.length; i++){ map.set(nums[i],i); }for(let i = 0; i < nums.length; i++){const diff = target - nums[i];
       if(map.has(diff) && map.get(diff) ! == i){return[i,map.get(diff)]; }}};Copy the code
/ * * *@param {number[]} nums
 * @param {number} target
 * @return {number[]}* Dynamic hash table method * time complexity: O(N), where N is the number of elements in the array * space complexity: O(N), where N is the number of elements in the array */
/** 92ms */
var twoSum3 = function (nums, target) {
   
    const map = new Map(a);for (let i = 0; i < nums.length; i++) {
        const num1 = nums[i];
        const num2 = target - nums[i];
        if(map.has(num2)){
            return [map.get(num2),i];
        }else{ map.set(num1,i); }}};Copy the code
/** 100 ms * Class violence lookup * time complexity: O(N^2), where N is the number of elements in the array * space complexity: O(N), where N is the number of elements in the array */
var twoSum4 = function(nums, target) {
  let temp = []
  for (let i = 0; i < nums.length; i++) {
    let dif = target-nums[i]
    if(temp[dif] ! = =undefined) {
      return[temp[dif], i] } temp[nums[i]] = i; }};Copy the code
/** 100 ms * Map method * create a new dictionary * nums values, loop through, if there are no appropriate values, return * time complexity: O(N), where N is the number of elements in array * space complexity: O(N), where N is the number of elements in array */
var twoSum6 = function(nums, target) {
    const map=new Map(a);const len=nums.length;
    for(let i=0; i<len; i++) {const targetNum=target-nums[i];// Get the difference
        if(map.has(nums[i])) 
        {
            return [map.get(nums[i]),i];// Determine if the current value is included in the map and return if it is
        }
        map.set(targetNum,i);// If not, put the difference into the map
    }
    console.log(map);
};
Copy the code

Their thinking

  • Enumeration of violence

    • Double loop
  • Hash table

    • Maintain a hash table, key is the number, value is the subscript, traverses the number group, determine whether (target value – current number) value exists in the hash table, if it does, return the corresponding subscript, does not exist, put the current number into the hash table