题目 Sum of pairs 链接:题目 思路 这道题解法很简单,用双层循环能够解决。但是会超出时间,所以需要将双循环简化成单循环。 简化: 双层循环耗时长,简化为单层能减少时间。 用集合来存放已经遍历过的元素能够减少耗时。 代码 def sum_pairs(ints,s): cache = set() for i in ints: other = s - i if other in cache: return [other,i] cache.add(i) 参考链接