File tree Expand file tree Collapse file tree 2 files changed +13
-0
lines changed
Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change 55# If complement is not in has table(seen), store the current number in hash table(seen) and continue checking the numbers
66# The algorithm uses a single loop that goes through each element in nums exactly one time.
77# If the array contains n elements, the loop runs n times → O(n).
8+ # We use a hash table (seen) to store numbers we've already visited.
9+ # seen could contain up to n − 1 entries, so its size grows linearly with input size → O(n).
10+
11+
12+
813class Solution :
914 def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
1015 seen = {}
Original file line number Diff line number Diff line change 1+ /*
2+
3+
14// Create a empty hash table(seen) and store numbers as a key and their indices as a value
25// Iterate through the given array(nums)
36// For each numbers in given array, calculate complement (target - num)
47// If complement is in hash table(seen), return array of index of the currnet number and index of complement in hash table(seen)
58// If complement is not in has table(seen), store the current number in hash table(seen) and continue checking the numbers
69// The algorithm uses a single loop that goes through each element in nums exactly one time.
710// If the array contains n elements, the loop runs n times → O(n).
11+ We use a hash table (seen) to store numbers we've already visited.
12+ seen could contain up to n − 1 entries, so its size grows linearly with input size → O(n).
13+
14+
15+ */
816
917function twoSum2 ( nums : number [ ] , target : number ) : number [ ] {
1018 const seen :{ [ key :number ] : number } = { } ;
You can’t perform that action at this time.
0 commit comments