Back to Browse

Leetcode | Easy | 1. Two Sum | Hash Table | Time complexity: O(n) | Javascript

147 views
Apr 22, 2023
9:49

Algorithm A simple implementation uses two iterations. In the first iteration, we add each element's value as a key and its index as a value to the hash table. Then, in the second iteration, we check if each element's complement (target−nums[i]target - nums[i]target−nums[i]) exists in the hash table. If it does exist, we return current element's index and its complement's index. Beware that the complement must not be nums[i]nums[i]nums[i] itself!

Download

0 formats

No download links available.

Leetcode | Easy | 1. Two Sum | Hash Table | Time complexity: O(n) | Javascript | NatokHD