Leetcode刷题记录本
ID: 1
点击查看代码
- 暴力破解法
class Solution(object):
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(len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
- 使用字典,即key-map
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
records = dict()
for index,value in enumerate(nums):
if target-value in records:
return [records[target-value],index]
records[value] = index
标签:记录本,target,nums,int,List,records,type,Leetcode,刷题
From: https://www.cnblogs.com/lwp-nicol/p/17617214.html