Skip to content

Two Sum

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of integers nums and a number target, find the two different positions whose values add up to target. Return their indices as a list in increasing order.

nums = [2, 7, 11, 15], target = 9    →  [0, 1]   (2 + 7)
nums = [3, 2, 4],      target = 6    →  [1, 2]   (2 + 4)
nums = [3, 3],         target = 6    →  [0, 1]

You may not use the same index twice.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Exactly one valid answer exists.