Accepted javaScript solution


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

  • 0
    G

    I cannot imagine a shorter or better solution, to be honest. I wonder though, if the array were enormous and the target number were too, would this still be the best? Probably.


Log in to reply
 

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