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 ba618d4 commit b2ef9efCopy full SHA for b2ef9ef
two-sum/printjin-gmailcom
@@ -1,6 +1,8 @@
1
class Solution:
2
def twoSum(self, nums, target):
3
- for i in range(len(nums)):
4
- for j in range(i + 1, len(nums)):
5
- if nums[i] + nums[j] == target:
6
- return [i, j]
+ num_map = {}
+ for i, num in enumerate(nums):
+ left = target - num
+ if left in num_map:
7
+ return [num_map[left], i]
8
+ num_map[num] = i
0 commit comments