
Published 2020. 1. 15. 00:06
반응형
문제 1. twoSum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
int 자료형으로 구성된 list의 요소끼리 서로 더했을 때 target이 되는 경우를 찾고, 해당하는 값의 index를 list로 반환하면 됩니다.
nums = [2, 7, 11, 15]
target = 9
def twoSum(nums, target):
check = {}
for i, num in enumerate(nums) :
value = target - num
if value in check:
return [check[value], i]
check[num] = i
return []
print(twoSum(nums, target))
http://github.com/Leo-bb/LeetCode
Leo-bb/LeetCode
Contribute to Leo-bb/LeetCode development by creating an account on GitHub.
github.com
반응형