我们如何找到以下程序的时间复杂度?对于下面的程序,时间复杂度应该是
O(N)
还是O(N*M)
还是O(N*M*M)
?
采取1:
O(N)
第二次:
O(N*M)
split
和 find
第三步:
O(N*M*M)
split
find
input_mails = ["[email protected]","[email protected]"]
for email in input_mails:
first_part, second_part = email.split("@")
position = email.find(".")
print(first_part)
给定程序的时间复杂度为
N*2M
~ O(N * M)
其中:
N: number of elements in the input array (input_mails)
M: length of the longest email address in the list
以下是操作如何影响时间复杂度:
N
元素:O(N)
O(M)
,因为拆分取决于最长电子邮件地址的长度。O(M)
,因为查找点还取决于最长电子邮件地址的长度。