Last active
March 27, 2021 13:52
-
-
Save hertonwork/0e6e0defb7f1c686298266edcb6c6611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. | |
* You may assume that each input would have exactly one solution, and you may not use the same element twice. | |
* You can return the answer in any order. | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} | |
*/ | |
function twoSum(nums, target) { | |
let index, left = 0; | |
while (left < nums.length - 1) { | |
index = nums.indexOf(target-nums[left],left+1) | |
if (index != -1) return [left, index]; | |
left++; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment