Simple Javascript Solution


  • 0
    R
    function twoSum(nums, target) {
      for (let i=0;i<nums.length;i++){
          for (let j=0;j<nums.length;j++){
              if (nums[i] + nums[j] === target && i !== j){
                  const out = [i, j];
                  return out;
              }
          }
      }
    };
    

  • 0
    C

    Hi Robbie,

    Yes, it's a simple javascript solution but initializing second loop counter equal to first counter plus one is better than checking every time within the condition.

    The statement says 'You may assume that each input would have exactly one solution.', It does not need to check same index twice.


Log in to reply
 

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