Skip to content

Commit 10b2e9e

Browse files
committed
refactor: improve comment formatting and clarity time/space complexity in twoSum implementations
1 parent f027594 commit 10b2e9e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

two-sum/hongseoupyun.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
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+
813
class Solution:
914
def twoSum(self, nums: List[int], target: int) -> List[int]:
1015
seen = {}

two-sum/hongseoupyun.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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

917
function twoSum2(nums: number[], target: number): number[] {
1018
const seen:{[key:number] : number} = {};

0 commit comments

Comments
 (0)