Accepted JavaScript O(n) Solution


  • 0
    J

    Here is my solution. Please do forgive indentation.

    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number[]}
     */
    var twoSum = function(nums, target) {
        let arrayToReturn = [];
        let k = 0;
        
        for(i = 0; k < nums.length; i++) {
            if (nums[i] + nums[k] === target && k !== i) {
                    arrayToReturn.push(k);
                    arrayToReturn.push(i);  
                return arrayToReturn;
            }
            
            if (i === nums.length) {
                i = 0;
                k++;
            }
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.