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 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
python代码
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
lls = 1
if len(s) == 0:
return 0
if len(s) == 1:
return 1
i = 0
j = 1
while j < len(s):
pos = s.find(s[j], i, j)
if pos != -1:
lls = max(lls, j-i)
i = pos + 1
j += 1
if s.find(s[len(s)-1], i, len(s)-1) == -1:
lls = max(lls, len(s)-i)
return lls
PS:这题有多种解法,博主这种虽然巧妙用了find()函数,但已经相当于直接暴力解了,据说用dict也可以解,而且效率更高,先留着以后有空来试一下
本文探讨了如何寻找给定字符串中最长的无重复字符子串,并提供了一种使用Python实现的方法。通过示例说明了不同输入情况下的解法。
702

被折叠的 条评论
为什么被折叠?



