简体   繁体   English

O(sumof(n)) 时间复杂度

[英]O(sumof(n)) time complexity

Question is simple and straightforward: is there such thing as a time complexity of问题简单明了:是否存在时间复杂度O(总和(n))

and is this the proper way to write it?这是正确的写法吗? I am asking because this program I wrote:我问是因为我写的这个程序:

    def check(self, front, s, back):
        if(s[front:back] == s[front:back][::-1]): 
            return s[front:back]
        else: 
            return self.check(front, s, back-1)

    def checkIter(self, front, s, back, longest):
        r = self.check(front, s, back)
        if(len(r) >= len(longest)): longest = r
        if(front < back):
            return self.checkIter(front+1,s,back, longest)
        else:
            return longest
        
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        return self.checkIter(0,s,len(s),"")

on input of longestPalindrome('cbbd') it runs ~10 times producing the ouputs:longestPalindrome('cbbd')输入上,它运行 ~10 次产生输出:

"cbbd" "cbb" "cb" "c" "bbd" "bb" "b" "bd" "b" "d" . "cbbd" "cbb" "cb" "c" "bbd" "bb" "b" "bd" "b" "d" On a simalar input of longestPalindrome('cbbda') gives: "cbbda" "cbbd" "cbb" "cb" "c" "bbda" "bbd" "bb" "b" "bda" "bd" "b" "da" "d" "a" So this can't be O(n)longestPalindrome('cbbda')的类似输入上给出: "cbbda" "cbbd" "cbb" "cb" "c" "bbda" "bbd" "bb" "b" "bda" "bd" "b" "da" "d" "a"所以这不可能是 O(n)

The specified sum is equal to n(n+1)/2 .指定的总和等于n(n+1)/2 Hence, it is in O(n^2) .因此,它在O(n^2)中。

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

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