728x90
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, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for idx in range(len(nums)):
have_to_find = target - nums[idx]
for idx2 in range(idx+1,len(nums)):
if nums[idx2]==have_to_find:
return [idx,idx2]
'hacking or software engineering skills > leetcode' 카테고리의 다른 글
6. ZigZag Conversion (0) | 2019.05.09 |
---|---|
5. Longest Palindromic Substring (0) | 2019.05.09 |
4. Median of Two Sorted Arrays (0) | 2019.05.09 |
3. Longest Substring Without Repeating Characters (0) | 2019.05.09 |
2. Add Two Numbers (0) | 2019.05.09 |