459. Repeated Substring Pattern

来源:互联网 发布:网页在线客服软件 编辑:程序博客网 时间:2024/04/30 03:47

代码比较简单,分为奇数偶数讨论。
如果有第19行,运行时间35ms;没有的话 39ms.

class Solution(object):    def repeatedSubstringPattern(self, str):        """        :type str: str        :rtype: bool        """        n=len(str)        m=n//2        if not n&1:            return str[:m]==str[m:]        else:            i=1            while i<=m:                if not n%i:                    if str==str[:i]*(n//i):                        return True                i+=1        return False
0 0