We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0332026 commit 909186eCopy full SHA for 909186e
scripts/algorithms/T/Two Sum/Two Sum.py
@@ -1,16 +1,11 @@
1
-# Runtime: 90 ms (Top 72.15%) | Memory: 15.1 MB (Top 64.41%)
+// Runtime: 2571 ms (Top 18.27%) | Memory: 17.10 MB (Top 86.21%)
2
3
class Solution:
4
-
5
def twoSum(self, nums: List[int], target: int) -> List[int]:
+ n = len(nums)
6
+ for i in range(n - 1):
7
+ for j in range(i + 1, n):
8
+ if nums[i] + nums[j] == target:
9
+ return [i, j]
10
+ return [] # No solution found
11
- arr = dict()
- # store the index of each number in the array
- for i,num in enumerate(nums):
- arr[num] = i
12
- # iterate one more time over the array to check if there is any element which complement is in the array
13
- for j, num in enumerate(nums):
14
- rest = target - num
15
- if arr.get(rest, j) != j:
16
- return [j, arr[rest]]
0 commit comments