关于找无重复最长字符串的问题
如图,我实在想不出来我的代码出啥问题了 s , m = input() . strip() , 0for i in range(len(s)):
e , n = s , 1
while len(e) == len(set(e)) and i + n < len(s):
n += 1
e = e + s
if n - 1 > m :
m = n - 1
print(m) class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
count = 0
string = ""
for i in range(len(s)):
if s not in string:
string += s
else:
count = len(string) if len(string) > count else count
exist_index = string.find(s)
string = string[(exist_index+1):] + s
else:
count = len(string) if len(string) > count else count
return count ZhKQYu 发表于 2022-8-16 21:30
我想问一下,break能跳出嵌套if不,刚刚将break缩进提前了一层,就通过了 叶墨沫 发表于 2022-8-16 21:54
我想问一下,break能跳出嵌套if不,刚刚将break缩进提前了一层,就通过了
当然可以的啊 ZhKQYu 发表于 2022-8-16 21:56
当然可以的啊
抱歉抱歉,我忽略了个小细节,break放里面的if了 本帖最后由 jackz007 于 2022-8-16 23:00 编辑
class Solution(str):
def lengthofLongestSubstring(self):
r = 0
for i in range(len(self)):
n = 0
while len(self) == len(set(self)) and i + n < len(self):
n += 1
if r < n:
r = n
return r
s = Solution(input())
print(s . lengthofLongestSubstring())
页:
[1]