思路:x+y = tsum ,那么y = tsum -x ,只需要遍历array,for x in array,判断tsum-x 是否in array即可。有的话加入列表中。array遍历结束后,用内置的sort进行排序即可
# -*- coding:utf-8 -*- class Solution: def FindNumbersWithSum(self, array, tsum): # write code here if not array or not tsum: return [] result = [] for i in array: if (tsum - i) in array: result.append([i, tsum - i]) if result: result.sort(key=lambda x: x[0] * x[1]) result = result[0] result.sort() return result return []