鉴于:
[1, 3, 46, 1, 3, 9]
找到这两者之间的不同组合,以获得尽可能多的目标编号=47。46+1和1+46是相同的。
我的代码:
def stockPairs(stocksProfit, target):
# Write your code here
print(stocksProfit)
#print(target)
count = 0
for t, sp in enumerate(stocksProfit):
#print(stocksProfit[t])
for l in range(1, len(stocksProfit)):
total = stocksProfit[t] + stocksProfit[l]
#print(total, stocksProfit[t], stocksProfit[l] )
if total == target :
count = count + 1
return count
预期结果是1,因为我只想要不同的组合
解决方案1:任意2个数字的组合
输出
解决方案2:列表中任意数量元素的组合
输出
注意:两种解决方案都有重复。需要通过逻辑删除重复元组
相关问题 更多 >
编程相关推荐