Difficulty: easy · Topic: hashing
Given an array of integers nums and an integer target, return the
indices of the two numbers that add up to target.
Each input has exactly one solution, and you may not use the same element twice. Return the indices in ascending order.
twoSum([2, 7, 11, 15], 9) -> [0, 1]
The obvious approach compares every pair, which is O(n²). There is an O(n) way.