Problem
LeetCode 1: Two Sum
Solution
| |
Explaination
The core idea here is x + y = z(sum) , y = z(sum) - x . We iterate over the list, each element becomes x, we do z(sum) - x and check it’s presense in the list. we are using sets for efficient membership checks.
References
Also see
- Concatenation of Array
- Contains Duplicate
- Valid Anagram
- Longest Common Prefix
- Group Anagrams
- Remove Element
- Majority Element
- Design HashSet
- Design HashMap
- Sort an Array
- Sort Colors
- Top K Frequent Elements
- Encode and Decode Strings
- Range Sum Query 2D Immutable
- Product of Array Except Self
- Valid Sudoku
- Longest Consecutive Sequence
- Best Time to Buy And Sell Stock II
- Majority Element II
- Subarray Sum Equals K
- First Missing Positive