Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:
Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
class Solution:
def lengthOfLongestSubstring(self, s):
“”\"
:type s: str
:rtype: int
“”\"
start = 0
end = 1
length = 0
for i in range(len(s)):
if s[i] in s[start:i]:
# if the current character is in the substring before
if length < len(s[start:end]):
length = len(s[start:end])
while(s[i] in s[start:i]):
start = start + 1
end = i + 1
# to make no repeat situation result no zero
if length < len(s[start:end]):
length = len(s[start:end])
return length
###########################
start=-1
max=0
d={}
for i in range(len(s)):
if s[i] in d and d[s[i]]>start:
start=d[s[i]]
d[s[i]]=i
else:
d[s[i]]=i
if i-start>max:
max=i-start
return max
#####################
maxx = 0
substr = {}
substr = [s[i] for i in range(len(s))]
for i,a in enumerate(s):
substr[i] = s[i]
if i < len(s)-1:
j = i+1
while j < len(s):
if s[j] in substr[i]:
j = len(s)
else:
substr[i] = substr[i] + s[j]
j = j + 1
le = [len(substr[i]) for i in range(len(substr))]
if len(le) != 0:
maxx = max(le)
return maxx
继续阅读与本文标签相同的文章
从能力开放走向生态,构建多媒体共享商业生态
-
使用nginx部署网站
2026-05-18栏目: 教程
-
Java的四种线程池的使用,以及自定义线程工厂
2026-05-18栏目: 教程
-
Leetcode之删除排序数组中的重复项
2026-05-18栏目: 教程
-
详细领悟ThreadLocal变量
2026-05-18栏目: 教程
-
MyBatis的底层实现原理!是动态代理的运用~
2026-05-18栏目: 教程
