需要考虑重复数据的问题,开始没有想到用双指针,后来解决了,是in在list和dict查找的效率问题,严格一点空间复杂度过不了
1、超时
n, k = map(int, input().split()) l = list(map(int, input().split())) for i in range(n-1): x = k - l[i] if x < l[i]: break if x in l: if i > 0 and l[i] == l[i-1] or l[i] == x and l.count(l[i]) == 1: continue print(l[i], x)
2、AC 136ms
n, k = map(int, input().split()) l = list(map(int, input().split())) m = {} for i in range(n): m[l[i]] = 1 if l[i] not in m.keys() else m[l[i]] + 1 for i in range(n-1): x = k - l[i] if x < l[i]: break if x in m.keys(): if i > 0 and l[i] == l[i-1] or l[i] == x and m[l[i]] == 1: continue print(l[i], x)