简体   繁体   English

toString时间和空间的复杂性

[英]toString time and space complexity

Given the following method: 给出以下方法:

public String toString()
{
    if (_head == null)
        return "";
    String S="";
    WordNode currentNode = _head;
    while (currentNode != null)
    {
       S+=currentNode.getWord()+" ";
       currentNode = currentNode.getNext();
    }   
    return S;

} }

What are time and space complexities? 什么是时间和空间复杂度? In Java, String is immutable object. 在Java中,String是不可变的对象。 How can it affect complexity? 它如何影响复杂性? Thanks. 谢谢。

Time complexity is O(n), where n is the number of nodes, because you iterate over each node once. 时间复杂度为O(n),其中n是节点数,因为您需要在每个节点上迭代一次。 Space complexity is actually O(n*m), where n is the number of nodes and m is the length of the largest string you will create (which is the last string which contains all the words). 空间复杂度实际上是O(n * m),其中n是节点数,m是将创建的最大字符串的长度(这是包含所有单词的最后一个字符串)。 This is because you create n strings, and string creation in java has memory usage proportional to the number of characters in the string (which in your case is maximum m). 这是因为您创建了n个字符串,并且java中的字符串创建具有与字符串中字符数成正比的内存使用量(在您的情况下,最大字符数为m)。 If you want to see exactly how much memory string creation uses, see this link: http://www.javamex.com/tutorials/memory/string_memory_usage.shtml . 如果要确切了解创建多少存储字符串,请访问以下链接: http : //www.javamex.com/tutorials/memory/string_memory_usage.shtml

Btw, you do not really need the if check in the beginning of your function because if the head is null, your while loop will not undergo any iterations and you will return an empty string anyway. 顺便说一句,您实际上并不需要在函数的开头进行if检查,因为如果head为null,则while循环将不会进行任何迭代,并且无论如何您都将返回一个空字符串。 Taking out this conditional will improve your time performance by a constant factor (but the time complexity will of course be the same). 排除此条件将使您的时间性能提高一个常数(但是时间复杂度当然会相同)。

The time complexity is O(L*N) where N is the number of nodes and L is the lenght of the resulting string: 时间复杂度为O(L*N) ,其中N是节点数, L是结果字符串的长度:

If all but the first node contains a string of length 1 and the first node contains a string L-2*(N-1)-1 . 如果除第一个节点之外的所有节点都包含一个长度为1的字符串,并且第一个节点包含一个字符串L-2*(N-1)-1 Then the strings stored in S have lengths in the iterations of the loop have lengths 然后,在循环的迭代中,存储在S的字符串的长度为

L-2*(N-1)
L-2*(N-2)
L-2*(N-3)
...
L

the sum of those values is in O(L*N) . 这些值的总和为O(L*N) Since allocating the strings takes time proportional to the sting size, the algorithm has time complexity O(N*L) . 由于分配字符串花费的时间与字符串大小成正比,因此该算法具有时间复杂度O(N*L)

The space complexity is O(L) since the stings stored in S have lengths in O(L) and intermediate results from previous iterations can be reclaimed by the garbage collector as soon as S is overwritten. 空间复杂度为O(L)因为存储在S的字符串的长度为O(L)并且一旦S被覆盖,垃圾回收器就可以回收先前迭代的中间结果。

You can improve this to space and time complexity O(L) by using a StringBuilder . 您可以使用StringBuilder将其改进为时空复杂度O(L)

public String toString()
{
    if (_head == null)
        return "";
    StringBuilder S=new StringBuilder();
    WordNode currentNode = _head;
    while (currentNode != null)
    {
       S.append(currentNode.getWord()).append(" ");
       currentNode = currentNode.getNext();
    }   
    return S.toString();

}

Each loop which iterate over n items has time complexity O(n). 迭代n个项目的每个循环的时间复杂度为O(n)。 When you nest loop in another loop 当您将循环嵌套到另一个循环中时

while(...) {
   while(...) {
    ...
   }
}

it became O(n^2) and so forth. 它变为O(n ^ 2)等等。

As you are using only one loop, your method time complexity is O(n). 由于仅使用一个循环,因此方法时间复杂度为O(n)。

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

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