LeetCode the hard way: 0001 Two Sum

Yan
May 18, 2021

https://leetcode.com/problems/two-sum/

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.
Photo by othmane ferrah on Unsplash

Analysis

Critical Knowledge

The basic idea of the problem is how to quickly look up a desired value. The fastest data structure is a hash map. https://runtimee.medium.com/practical-algorithms-tier-0-hash-map-275610139a92

A hash map has the property of time complexity of O(1) with extra space O(N). It is a common trick to trade space (more memory) for speed (less time).

Q.E.D.

--

--