deftwoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j] == target: return [i, j]
1 2 3 4 5 6 7 8 9 10 11
deftwoSum(self, nums: List[int], target: int) -> List[int]: temp = [(n, i) for i, n in enumerate(nums)] temp.sort() i, j = 0, len(temp) - 1 while i < j: if temp[i][0] + temp[j][0] < target: i += 1 elif temp[i][0] + temp[j][0] > target: j -= 1 else: return [temp[i][1], temp[j][1]]
1 2 3 4 5 6
deftwoSum(self, nums: List[int], target: int) -> List[int]: nums_dict = {} for i, n in enumerate(nums): if target - n in nums_dict: return [nums_dict[target - n], i] nums_dict[n] = i