|

楼主 |
发表于 2021-10-23 14:11:24
|
显示全部楼层
class Solution:
def longestCommonPrefix(self, strs: list[str]) -> str:
start = 1
stillmatch = True
while start <= len(strs[0]):
# 将字符串数组第一个字符串之后的字符串分别与第一个字符比较
for j in range(1, len(strs)):
if strs[0][:start] != strs[j][:start]:
stillmatch = False
break
# 一旦发现不匹配则可以退出循环
if not stillmatch:
break
# 更新比较的字符串前缀的位置
start += 1
return strs[0][:start - 1]
num = eval(input("请输入一串字符串:"))
nums = num.longestCommonPrefix(num)
print(nums)
报错:
Traceback (most recent call last):
File "C:\Users\mate\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\second.py", line 43, in <module>
class Solution:
File "C:\Users\mate\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\second.py", line 44, in Solution
def longestCommonPrefix(self, strs: list[str]) -> str:
TypeError: 'type' object is not subscriptable |
|