We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 533e8b9 commit 069b837Copy full SHA for 069b837
ArraysAndHashing/Easy/1_Two_Sum.py
@@ -15,6 +15,11 @@
15
# Time Complexity: O(n^2)
16
# Space Complexity: O(1)
17
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
+
23
class Solution:
24
def twoSum(self, nums, target):
25
for i in range(len(nums)):
@@ -26,6 +31,12 @@ def twoSum(self, nums, target):
26
31
# Time Complexity: O(N)
27
32
# Space Complexity: O(N)
28
33
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
29
40
30
41
42
prev = {}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments