Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 相同的代码,但其中一个作为输出超出了时间限制,而另一个工作正常_Python - Fatal编程技术网

Python 相同的代码,但其中一个作为输出超出了时间限制,而另一个工作正常

Python 相同的代码,但其中一个作为输出超出了时间限制,而另一个工作正常,python,Python,第一个代码是我写的,但是在执行它的时候,我被超过了时间限制 class Solution: def isHappy(self, n: int) -> bool: l=set() while(n not in l): l.add(n) x=0 while n: s=str(n) for i in s:

第一个代码是我写的,但是在执行它的时候,我被超过了时间限制

class Solution:
    def isHappy(self, n: int) -> bool:
        l=set()
        while(n not in l):
            l.add(n)
            x=0
            while n:
                s=str(n)
                for i in s:
                    x+=(int(i)*int(i))
                n=x
            if(n==1):
                return True
        return False
这是第二段代码,在执行时,它运行得非常好

class Solution:
    def isHappy(self, n: int) -> bool:
        visit = set()
        while n not in visit:
            visit.add(n)
            n = sumofSquares(n)
            if n == 1: 
                return True
        return False

def sumofSquares(n):
    output = 0
    while n:
        for i in str(n):
            output += int(i) * int(i)
        return output

大家能告诉我这两个代码的时间复杂度之间的区别,以及为什么会发生这种情况吗?

您正在编写的代码有一个无限循环,请尝试(手动)运行您的代码,并使用n=1运行第二个代码

以下是工作代码:

def isHappy(n: int) -> bool:
        visit = set()
        while n not in visit:
            visit.add(n)
            square_sum = 0
            for i in str(n):
                square_sum += int(i)**2
            n = square_sum
            if n == 1: 
                return True
        return False

for i in range(20):
    print(i, isHappy(i))
# prints 'True' for:
# 1, 7, 10, 13, 19

您正在编写的代码执行无限循环,请尝试(手动)运行您的代码,并使用n=1运行第二个代码

以下是工作代码:

def isHappy(n: int) -> bool:
        visit = set()
        while n not in visit:
            visit.add(n)
            square_sum = 0
            for i in str(n):
                square_sum += int(i)**2
            n = square_sum
            if n == 1: 
                return True
        return False

for i in range(20):
    print(i, isHappy(i))
# prints 'True' for:
# 1, 7, 10, 13, 19

无限循环发生在此块中:

        while n:
            s=str(n)
            for i in s:
                x+=(int(i)*int(i))
            n=x
然而,等效代码也有一个无限循环,但能够退出,因为
return
退出函数(因此也退出
while循环
):


无限循环发生在此块中:

        while n:
            s=str(n)
            for i in s:
                x+=(int(i)*int(i))
            n=x
然而,等效代码也有一个无限循环,但能够退出,因为
return
退出函数(因此也退出
while循环
):


请阅读。您还可以使用它来帮助逐步可视化代码的执行。这不是时间复杂性。这是无限循环。请阅读。您还可以使用它来帮助逐步可视化代码的执行。这不是时间复杂性。这是无限循环。嗨@BeChillerToo,你的代码工作正常!但为什么会有反对票?我已经给了你一个倒过来的投票:-)谢谢,我不知道我是怎么跳出来的嗨@BechillerTo,你的代码是有效的!但为什么会有反对票?我给了你一个倒过来的投票:-)谢谢,不知道我是怎么跳的谢谢,不知道我是怎么跳的谢谢,不知道我是怎么跳的