문제
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].
풀이
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
ret = []
for first in range(len(nums)) :
for second in range(first+1,len(nums)) :
if nums[first] + nums[second] == target :
return [first, second]
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
20. Valid Parentheses (0) | 2020.05.04 |
---|---|
9. Palindrome Number (0) | 2020.05.04 |
14.Longest Common Prefix (0) | 2020.05.03 |
13. Roman to Integer (0) | 2020.05.02 |
7.Reverse Integer (0) | 2020.05.02 |