The sum of two numbers (JS)

Insert picture description here

Idea: Create an array to copy from the original array for comparison.
Then the comparison is returned if it is equal to the target value.

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    
    
    //数组的每一个下标都遍历.
    var arr=nums;
    var arr1=[];
    var d=0;
    for(var i=0;i<nums.length;i++)
    {
    
    
       
        for(var j=i+1;j<arr.length;j++)
        {
    
    
            if(nums[i]+arr[j]==target)
               {
    
    
                    return [i,j];
               }
        }
        
    }
};

Guess you like

Origin blog.csdn.net/qq_37805832/article/details/115221043