bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1. Two Sum

1. Two Sum

Leetcode Solutions

One-pass Hash Table

  1. Initialize an empty hash table (dictionary).
  2. Iterate through the array of numbers. a. For each element, calculate its complement by subtracting it from the target. b. Check if the complement is present in the hash table. i. If present, return the current index and the index of the complement from the hash table. c. If the complement is not present, add the current element's value and index to the hash table.
  3. Continue until the end of the array.
UML Thumbnail

Two-pass Hash Table

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR