首页 > 解决方案 > Leetcode:提交的函数产生意外的输出

问题描述

当我提交以下解决方案时

class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
    li = []
    i = 0
    for ch in t:
        if ch == s[i]:
            i+=1
            #debug
            li.append(ch)
    if s == ''.join(li):
        return "true"
    else:
        return "false"

在 leetcode 中,解决方案失败了,因为它显示了输出,true 如这里所示。但是我的机器上这个测试用例的输出false是正确的和预期的输出。我已经尝试在各种论坛上找到解决方案,并且已经尝试了leetcode这篇文章提供的所有推荐解决方案,但它也不起作用。

解决方案失败的测试用例:

"axc" "ahbgdc"

问题可以在 leetcode 上找到

标签: pythonalgorithm

解决方案


False != 'false',并且每个非空字符串(包括'false')的计算结果为True

只需返回TrueFalse(布尔值,如类型提示所建议的),而不是字符串。

def isSubsequence(self, s: str, t: str) -> bool:
    li = []
    i = 0
    for ch in t:
        if ch == s[i]:
            i+=1
            #debug
            li.append(ch)
    if s == ''.join(li):
        return True
    else:
        return False

推荐阅读