Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
625 B
8
Indexable
Never
// Solution for TwoSum problem with linear complexity
// https://leetcode.com/problems/two-sum
Input nums, target #enter
Create a map of numbers and indexes #group
	numIndexMap = new Map()
	let i = 0
	i < nums.length #if #id:mapLoop
		numIndexMap.set(nums[i], i)
		i++ #goto:mapLoop
Find the indexes #group
	let j = 0
	j < nums.length #if #id:evalLoop
		let other = target - nums[j]
		!numIndexMap.has(other) #if
			continue #goto:increment
		let otherIndex = numIndexMap.get(other)
		otherIndex === j #if
			continue #goto:increment
		return [j, otherIndex] #exit
		j++ #goto:evalLoop #id:increment
	return empty array #exit