Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
Subscribe to see which companies asked this question
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
result = []
test_start = 0
test_end = 0
if len(s) == 1:
return 1
while True:
test_end += 1
if test_end < len(s):
if s[test_end] in s[test_start:test_end]:
result.append(s[test_start:test_end])
test_start = s[test_start:test_end].index(s[test_end]) + test_start + 1
else:
result.append(s[test_start:])
break
max_len = 0
for str_temp in result:
if len(str_temp) > max_len:
max_len = len(str_temp)
return max_len
这道题目主要是各种边界条件比较坑,思考的时候一定要冷静,想清楚在开始编程序