简体   繁体   English

我对这个算法的时间复杂度分析是否正确?

[英]Am I right in my time complexity analysis for this algorithm?

Long story short, I am looking to verify if my time complexity for following code is correct.长话短说,我正在寻找验证以下代码的时间复杂度是否正确。

class Solution {
    public String reverseWords(String s) {
        int len = s.length(); int i=len-1, j=0;
        StringBuilder answer= new StringBuilder();
        while(i!=-1) {
            if(s.charAt(i)==' ') i--;
            else {
                j=i;
                while(j-1!=-1 && s.charAt(j-1)!=' ' ) j--; //j++;
                answer.append(s.substring(j, i+1)).append(' ');
                i=j-1;
            }
        }
        return answer.toString().trim(); //trim last index's space
    }
}

Constraints:约束:

  • 1 <= s.length <= 10^4
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '. s包含英文字母(大写和小写)、数字和空格 ' '。
  • There is at least one word in s s中至少有一个词

Assuming string length is N. My analysis is to further assume there are K spaces in string S and assume there are L different space separated words in it, as per given constraints we can conclude that L>=1 and K>=0.假设字符串长度为 N。我的分析是进一步假设字符串 S 中有 K 个空格,并假设其中有 L 个不同的空格分隔的单词,根据给定的约束我们可以得出 L>=1 和 K>=0 的结论。

Thus the average length of each word is NK/L, outer loop runs thus, K+L times, and inner loop runs independent of outer for 2*(NK)/L times making total complexity as 2(NK)/L + K+L which is still order of O(N).因此,每个单词的平均长度为 NK/L,因此外部循环运行 K+L 次,内部循环独立于外部运行 2*(NK)/L 次,总复杂度为 2(NK)/L + K +L 仍然是 O(N) 的阶数。

Am I making any wrong assumptions?我做了任何错误的假设吗? Is there a way to simplify this formula of 2*(NK)/L + K+L有没有办法简化这个公式 2*(NK)/L + K+L

I'm pretty sure that the runtime is O(n).我很确定运行时间是 O(n)。

Although I'm not too sure what the N in your formula is, the general rule for simplifying is looking at the largest value.虽然我不太确定公式中的 N 是什么,但简化的一般规则是查看最大值。 I will assume that N is the largest for you.我会假设 N 对你来说是最大的。 The "+K+L" is negligible so we have 2*(NK)/L. “+K+L”可以忽略不计,所以我们有 2*(NK)/L。 Distributing: (2/L)N - (2/L)K.分布:(2/L)N - (2/L)K。 We drop constants and take the largest one so it would still be O(n)我们去掉常数并取最大的常数,所以它仍然是 O(n)

In my opinion, an easier way to look at the runtime is to just look at how many times your code runs through the string.在我看来,查看运行时的一种更简单的方法是查看代码在字符串中运行的次数。 Your first while goes until it triggers the else.你的第一个 while 一直持续到它触发 else。 Starting from the index you stopped at, the second loop keeps counting until it stops.从您停止的索引开始,第二个循环一直计数直到它停止。 From there, you change the original i to the j and keep going through.从那里,您将原始 i 更改为 j 并继续进行。 I think the code looks at where i stops twice but that isn't enough to change the complexity (a fix would just be to set j to i - 1 since you know the char at i is not a space already).我认为代码会查看 i 两次停止的位置,但这不足以改变复杂性(解决方法是将 j 设置为 i - 1 ,因为您知道 i 处的 char 已经不是空格)。

As a short side note, N typically denotes the length of the input, whether it be string length or array size.作为一个简短的说明,N 通常表示输入的长度,无论是字符串长度还是数组大小。

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

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