为什么用一行代码更改执行时间?

问题描述

这是我的第一个代码

def isAnagram(s,t):
    if len(s)!=len(t):
        return False
    for i in s:
        if s.count(i)!=t.count(i):
            return False
    return True

这是我的第二个代码

def isAnagram(s,t):
    if len(s)!=len(t):
        return False
    for i in set(s):
        if s.count(i)!=t.count(i):
            return False
    return True

这是我的第三个代码

def isAnagram(s,t):
    if len(s)!=len(t):
        return False
    for i in set(s[:]):
        if s.count(i)!=t.count(i):
            return False
    return True

我不明白为什么在第 4 行用 s 替换 set(s) 需要更少的执行时间,用 set(s[:]) 替换甚至比其他两个语句更好。

谁能帮我知道为什么会这样?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)