原题
https://leetcode.com/problems/two-sum
题干
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 i in range(len(nums)): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target: return i,j
解
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ lookup = {} for i,num in enumerate(nums): if target - num in lookup: return lookup[target-num],i else: lookup[num] = i
导语
你千万别跟任何人谈任何事情。你只要一谈起,就会想念起每一个人来,我只知道我很想念我所谈到的每一个人。
——J·D·塞林格《麦田里的守望者》
现代向RIMAC投资9000万美元!
沃达丰7月3日推出5G
▼立即加星标,每天看好文▼
1
2
极客学习空间
新媒体人都在关注
喜欢,就为我标星
我今天才知道,我之所以漂泊就是在向你靠近。
--《廊桥遗梦》