【发布时间】:2018-06-01 13:28:36
【问题描述】:
这段代码工作效率很高,但是如果我用注释行替换这两行(将值分配给A 和B),通过某些测试用例需要更多时间。
约束:n,m in range(10^5),arr 中的任何整数都在range(10^9) 中
请解释一下。
n, m = input().split()
arr=list(map(int,input().split()))
A = set(map(int,input().split())) #list(map(int,input().split()))
B = set(map(int,input().split())) #list(map(int,input().split()))
count=0
for i in range(len(arr)):
if arr[i] in A:
count+=1
elif arr[i] in B:
count+=-1
print(count)
【问题讨论】:
-
测试列表中的成员资格是
O(n),其中n是列表的长度。测试集合中的成员本质上是O(1)。在列表的情况下,它一次搜索每个列表元素。在集合的情况下,它正在执行哈希查找。 -
先读这个帖子list vs set;其次,您为什么三次使用相同的语句
map(int,input().split())?还阅读了有关 python 操作的TimeComplexity。
标签: python python-3.x