All files / leetCode 0001.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  8x 1x   7x 7x   7x 20x   20x 6x 6x 6x     14x     1x    
export default function twoSum(nums: number[], target: number): number[] {
  if (nums.length <= 1)
    return []
 
  const indices = new Map<number, number>()
  const solution: number[] = []
 
  for (let i = 0; i < nums.length; i++) {
    const rest = target - nums[i]
 
    if (indices.has(rest)) {
      solution.push(indices.get(rest) as number)
      solution.push(i)
      return solution
    }
 
    indices.set(nums[i], i)
  }
 
  return solution
}