Given an array of integers nums and and integer target, return the indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
2개의 nums 엘레멘트의 합으로 target 값을 만들기
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
target 값에서 한 엘레멘트를 빼고, 뺀 값이 nums배열에 있으면 된다
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
ret = []
for i, num in enumerate(nums):
n = target - num
if n in nums:
idx = nums.index(n)
if i != idx:
ret.append(i)
ret.append(idx)
break
return ret