简体   繁体   English

python function的时空复杂度

[英]Time and Space Complexity of python function

Can you please help me with how to calculate space and time complexity for below code你能帮我计算一下下面代码的空间和时间复杂度吗

def isPalindrome(string):
    # Write your code here.
    string1=string[::-1]
    if string1==string:
        return True
    else :
        return False

The easiest way to find complexity is to go line by line and under each operation.查找复杂度的最简单方法是逐行逐行逐项地对 go 进行操作。

Let's start with the first line让我们从第一行开始

string1=string[::-1]

This is a string slicing operation, which reverses the string and according to this , it takes time proportional to the number of characters which is being copied, in this case (your code) it is the whole string, hence it will be O(n) This is just line 1. Let's move ahead这是一个字符串切片操作,它反转字符串,根据这个,它所花费的时间与被复制的字符数成正比,在这种情况下(你的代码)它是整个字符串,因此它将是 O(n ) 这只是第 1 行。让我们继续前进

if string1==string:

here we are doing a string comparison, in the condition section of the if statement.在这里,我们在 if 语句的条件部分进行字符串比较。 according to this , it is again O(n) for line 2 据此,第 2 行再次为 O(n)

now, the following lines are just return and else block which will be done in constant time ie O(1)现在,以下几行只是 return 和 else 块,这将在恒定时间内完成,即 O(1)

hence for the total complexity, we just sum up all the line's complexity.因此,对于总复杂度,我们只是总结了所有线路的复杂度。 ie O(n) + O(n) + O(1) + O(1)即 O(n) + O(n) + O(1) + O(1)

you can refer to this to learn more about simplifying it.您可以参考this以了解有关简化它的更多信息。

So the final time complexity will be O(n)所以最终的时间复杂度将是 O(n)

This function can be broken down into complexity of its sub-processes.这个 function 可以分解为其子过程的复杂性。 In calculating this time complexity, let the amount of characters in string be n ( n = len(string ) in Python terms).在计算这个时间复杂度时,让string中的字符数为n (在 Python 术语中, n = len(string ))。 Now, let's look at the 2 sub-processes:现在,让我们看一下 2 个子流程:

  1. Traverse all characters in string in reverse order and assign to string1 (this is done by string1=string[::-1] ) - O(n) linear time since there are n characters in string .以相反的顺序遍历string中的所有字符并分配给string1 (这是由string1=string[::-1]完成的) - O(n)线性时间,因为string中有n 个字符。
  2. Compare if string1 == string - O(n) linear time because n characters in each string will be compared to each other, so that is n 1-to-1 comparisons.比较 if string1 == string - O(n)线性时间,因为每个字符串中的 n 个字符将相互比较,所以这是n一对一的比较。

Therefore, the total time complexity is O(n) + O(n) where n is len(string) .因此,总时间复杂度为 O(n) + O(n)其中nlen(string) In shorter terms, we simplify this to mean O(n) complexity .简而言之,我们将其简化为 O(n) 复杂度

The following two operations decides the time complexity of the above code:以下两个操作决定了上述代码的时间复杂度:

  1. With the below operation you are creating a copy of the list in the reversed manner which takes O(n) time and O(n) space:通过以下操作,您将以相反的方式创建列表的副本,这需要O(n)时间和O(n)空间:

     string1 = string[::-1]
  2. Checking the strings in the below line for equality again takes O(n) operations as you need to compare all the characters in the worst case:再次检查下一行中的字符串是否相等需要O(n)操作,因为您需要在最坏的情况下比较所有字符:

     if string1==string:

From the above, we can conclude the following:综上所述,我们可以得出以下结论:

Time complexity: O(n)时间复杂度: O(n)

Space complexity: O(n)空间复杂度: O(n)

where n represents the length of the input string.其中n表示输入字符串的长度。

You would also like go through this document which summarizes the time complexities for different operations in Python.您还想通过本文档了解 go,该文档总结了 Python 中不同操作的时间复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM