class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
result = {}
for i in range(len(nums)):
if target - nums[i] not in result:
result[nums[i]] = i
else:
return [result[target - nums[i]], i]
return [-1, -1]
Leetcode 之 Two Sum
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 问题:Given a Binary Search Tree and a target number, return...
- Time:2019/4/1Title:Two SumDifficulty: simpleAuthor:小鹿 题目一...
- 【题目描述】给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。函数应该返回这两个下标值 i...
- 167. Two Sum II - Input array is sorted 这是leetCode第167题 题...
- Problem Given a Binary Search Tree and a target number, r...