我想在 python3 中使用“枚举”来解决以下任务
枚举的工作方式如下所示
nums=(2,7,1,15) # a tuple
for num in enumerate(nums):
print(num, 'hello')
#output
#(0, 2) hello #enumarate(nums) = (0,2)
#(1, 7) hello
#(2, 1) hello
#(3, 15) hello
for count, num in enumerate(nums):
print(num, 'hello')
# Output
#2 hello here count=0 but not displayed
#7 hello here count=1 but not displayed
#1 hello here count=2 but not displayed
#15 hello here count=3 but not displayed
使用上述原理,给定一个由 n 个整数组成的数组 nums,在 nums 中是否存在元素 a、b、c 使得 a + b + c = 目标总和?在数组中找到所有唯一的三元组,其总和 = 目标总和。
目标总和 =10 的解集是:
[
[0,1,2]
]
其中第 0 个索引处的 num+第 1 个索引处的 num+第 2 个索引处的 num (7+2+1=10)。