题目:
给定一个字符串 s
,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
示例 2:
输入: s = "bbbbb"
输出: 1
示例 3:
输入: s = "pwwkew"
输出: 3
class Solution(object):
def lengthOfLongestSubstring(self, s):
i = 0
j = 0
res = 0
if len(s)==0:
return 0
elif len(s)==1:
return 1
else:
while i<len(s):
temp = s[i]
k = i
tag =True
count = 1
while k-1>=j: # 判断当前字符temp有没有在前面出现
if temp!=s[k-1]:
k -= 1
count += 1
if count>res:
res=count
else:
x = i - j
if x>res:
res = x
j += 1
i = j
tag = False
break
if tag:
i += 1
return res
这个代码超出时间限制了....
如果没有时间限制应该没问题。。。
算了,看看别人是怎么想的