(python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is .

Given "bbbbb", the answer is "b", with the length of .

Given "pwwkew", the answer is "wke", with the length of . Note that the answer must be a substring, "pwke" is a subsequence and not a substring

ANSWER:

 class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if s:
i=
max_sub =s[]
len_s=len(s)
while i<len(s):
j=i+
for index in range(j,len_s):
if s[index] not in s[i:index]:
if len(s[i:index+])>len(max_sub):
max_sub=s[i:index+]
else:
break
i+=
return len(max_sub)
else:
return

code

 def main(s):
if s:
i=
max_sub =s[]
len_s=len(s)
while i<len(s):
j=i+
for index in range(j,len_s):
if s[index] not in s[i:index]:
if len(s[i:index+])>len(max_sub):
max_sub=s[i:index+]
else:
break
i+=
return len(max_sub)
else:
return
if __name__ == '__main__':
s='pwwke'
print(main(s))

调试代码

上一篇:SQL Server执行计划那些事儿(1)——哈希、合并、嵌套联接的选择


下一篇:bzoj1001