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 849cf91 commit 7720effCopy full SHA for 7720eff
two-sum/chrisjune.py
@@ -3,9 +3,9 @@
3
4
class Solution:
5
def twoSum(self, nums: List[int], target: int) -> List[int]:
6
+ nums_dic = {num: idx for idx, num in enumerate(nums)}
7
for i in range(len(nums)):
- for j in range(len(nums)):
8
- if i == j:
9
- continue
10
- if nums[i] + nums[j] == target:
11
- return [i, j]
+ remain = target - nums[i]
+ exists_idx = nums_dic.get(remain)
+ if exists_idx and exists_idx != i:
+ return i, exists_idx
0 commit comments