Skip to content

Instantly share code, notes, and snippets.

@hertonwork
Last active March 27, 2021 13:52
Show Gist options
  • Save hertonwork/0e6e0defb7f1c686298266edcb6c6611 to your computer and use it in GitHub Desktop.
Save hertonwork/0e6e0defb7f1c686298266edcb6c6611 to your computer and use it in GitHub Desktop.
/**
* 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