??給定一個整數(shù)數(shù)組 nums 和一個目標(biāo)值 target,請你在該數(shù)組中找出和為目標(biāo)值的那 兩個 整數(shù),并返回他們的數(shù)組下標(biāo)。
??你可以假設(shè)每種輸入只會對應(yīng)一個答案。但是,你不能重復(fù)利用這個數(shù)組中同樣的元素。
示例:
給定 nums = [2, 7, 11, 15], target = 9
因?yàn)?nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
解題思路1(暴力法):使用兩層循環(huán)進(jìn)行查找匹配,每次都循環(huán)的是整個列表,效率比較低。
LeetCode中提交執(zhí)行結(jié)果:超出時間限制。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(len(nums)):
if target == nums[i] + nums[j] and i != j:
return [i, j]
else:
continue
return None
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 11], 18))
print(Solution().twoSum([25, 16, 13, 18, 8, 10], 23))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[0, 2]
[1, 3]
[2, 5]
None
解題思路2(暴力法優(yōu)化版):使用兩層循環(huán)進(jìn)行查找匹配,第二層只循環(huán)第一層還未循環(huán)的列表元素,效率比思路1高些。
LeetCode中提交執(zhí)行結(jié)果:超出時間限制。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if target == nums[i] + nums[j]:
return [i, j]
else:
continue
return None
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 11], 18))
print(Solution().twoSum([25, 16, 13, 18, 8, 10], 23))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[0, 2]
[1, 3]
[2, 5]
None
解題思路3:通過單循環(huán),依次計算出列表中每個元素與target的差值(target-nums[i]),并判斷該值(target-nums[i])是否在列表(僅查找還未循環(huán)的列表元素)中,如果在,就通過列表的index()方法獲取該值的索引值,如果不在,就繼續(xù)循環(huán),直到循環(huán)完整個列表。
需要注意:在通過index()方法獲取索引值時,需要考慮列表中存在相同元素的情況。
LeetCode中提交執(zhí)行結(jié)果-執(zhí)行用時:1336 ms,內(nèi)存消耗:14.2 MB。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
num = target - nums[i]
if num in nums[i+1:len(nums)]:
return [i, nums[i+1:len(nums)].index(num)+ i + 1]
else:
continue
return None
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 11], 18))
print(Solution().twoSum([25, 16, 13, 18, 8, 10], 23))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[0, 2]
[1, 3]
[2, 5]
None
解題思路4(哈希表法):通過迭代將元素添加到哈希表(使用字典模擬哈希表)中,同時我們比較該元素的對應(yīng)元素是否已經(jīng)存在于哈希表中,如果存在,我們直接返回答案。
LeetCode中提交執(zhí)行結(jié)果-執(zhí)行用時:36 ms,內(nèi)存消耗:14.8 MB。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for index, value in enumerate(nums):
if target - value in hashmap:
return [hashmap[target - value], index]
else:
hashmap[value] = index
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 11], 18))
print(Solution().twoSum([25, 16, 13, 18, 8, 10], 23))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[0, 2]
[1, 3]
[2, 5]
None
進(jìn)階需求(列表中存在多對滿足要求的值):
??給定一個整數(shù)數(shù)組 nums 和一個目標(biāo)值 target,請你在該數(shù)組中找出和為目標(biāo)值的那 兩個 整數(shù),并返回他們的數(shù)組下標(biāo)。
??注意:不能重復(fù)利用這個數(shù)組中同樣的元素。
示例:
給定 nums = [5, 16, 12, 14, 19, 8, 10, 12], target = 24
因?yàn)?nums[0] + nums[4] = 5 + 19 = 24,nums[1] + nums[5] = 16 + 8 = 24,nums[2] + nums[7] = 12 + 12 = 24,nums[3] + nums[6] = 14 + 10 = 24。
所以返回 [[0, 4], [1, 5], [3, 6], [2, 7]]
解題思路1:通過單循環(huán),依次計算出列表中每個元素與target的差值(target-nums[i]),并判斷該值(target-nums[i])是否在列表(僅查找還未循環(huán)的列表元素)中,如果在,就通過列表的index()方法獲取該值的索引值,如果不在,就繼續(xù)循環(huán),直到循環(huán)完整個列表。
需要注意:在通過index()方法獲取索引值時,需要考慮列表中存在相同元素的情況。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
res_list = []
for i in range(len(nums)):
num = target - nums[i]
if num in nums[i+1: len(nums)]:
res_list.append([i, nums[i+1: len(nums)].index(num)+ i + 1])
else:
continue
return res_list
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 2], 9))
print(Solution().twoSum([5, 16, 12, 14, 19, 8, 10, 12], 24))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[[0, 2]]
[[1, 3]]
[[0, 4], [1, 5], [2, 7], [3, 6]]
[]
解題思路2(哈希表法):通過迭代將元素添加到哈希表(使用字典模擬哈希表)中,同時我們比較該元素的對應(yīng)元素是否已經(jīng)存在于哈希表中,如果存在,我們直接返回答案。
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
res_list = []
for index, value in enumerate(nums):
if target - value in hashmap:
res_list.append([hashmap[target - value], index])
else:
hashmap[value] = index
return res_list
print(Solution().twoSum([5, 7, 5, 11], 10))
print(Solution().twoSum([9, 7, 3, 2], 9))
print(Solution().twoSum([5, 16, 12, 14, 19, 8, 10, 12], 24))
print(Solution().twoSum([7, 1, 6, 12, 0], 14))
運(yùn)行結(jié)果:
[[0, 2]]
[[1, 3]]
[[0, 4], [1, 5], [3, 6], [2, 7]]
[]