File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ # Create a empty hash table(seen) and store numbers as a key and their indices as a value
2+ # Iterate through the given array(nums)
3+ # For each numbers in given array, calculate complement (target - num)
4+ # If complement is in hash table(seen), return array of index of the currnet number and index of complement in hash table(seen)
5+ # If complement is not in has table(seen), store the current number in hash table(seen) and continue checking the numbers
6+ #
17class Solution :
28 def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
39 seen = {}
Original file line number Diff line number Diff line change 1+ // # Create a empty hash table(seen) and store numbers as a key and their indices as a value
2+ // # Iterate through the given array(nums)
3+ // # For each numbers in given array, calculate complement (target - num)
4+ // # If complement is in hash table(seen), return array of index of the currnet number and index of complement in hash table(seen)
5+ // # If complement is not in has table(seen), store the current number in hash table(seen) and continue checking the numbers
6+ //
7+
8+ function twoSum2 ( nums : number [ ] , target : number ) : number [ ] {
9+ const seen :{ [ key :number ] : number } = { } ;
10+ for ( let i = 0 ; i < nums . length ; i ++ ) {
11+ let complement = target - nums [ i ]
12+ if ( complement in seen ) {
13+ return [ seen [ complement ] , i ]
14+ }
15+ seen [ nums [ i ] ] = i
16+ }
17+ return [ ]
18+ } ;
You can’t perform that action at this time.
0 commit comments