Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 069b837

Browse files
1. Two Sum
1 parent 533e8b9 commit 069b837

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

‎ArraysAndHashing/Easy/1_Two_Sum.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
# Time Complexity: O(n^2)
1616
# Space Complexity: O(1)
1717

18+
'''
19+
In the below approach, we used two for loops to iterate through the array and check if the sum of the two numbers is equal to the target.
20+
If it is, we return the indices of the two numbers.
21+
'''
22+
1823
class Solution:
1924
def twoSum(self, nums, target):
2025
for i in range(len(nums)):
@@ -26,6 +31,12 @@ def twoSum(self, nums, target):
2631
# Time Complexity: O(N)
2732
# Space Complexity: O(N)
2833

34+
'''
35+
This approach uses a dictionary to store the difference between the target and the current number as the key and the index as the value.
36+
We then iterate through the array and check if the current number is in the dictionary.
37+
If it is, we return the index of the current number and the value of the dictionary.
38+
'''
39+
2940
class Solution:
3041
def twoSum(self, nums, target):
3142
prev = {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /