您的位置:首页 > 其它

[leetcode]无重复字符的最长子串

2018-08-28 21:48 429 查看
给定一个字符串,找出不含有重复字符的最长子串的长度。

示例 1:

输入: "abcabcbb"
输出: 3
解释: 无重复字符的最长子串是 [code=plain]"abc",其
长度为 3。[/code]
示例 2:

输入: "bbbbb"
输出: 1
解释: 无重复字符的最长子串是 [code=plain]"b"
,其长度为 1。[/code]
示例 3:

输入: "pwwkew"
输出: 3
解释: 无重复字符的最长子串是 [code=plain]"wke"
,其长度为 3。
请注意,答案必须是一个子串
"pwke"
是一个子序列 而不是子串。[/code]
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
s_len = len(s)
max_len = 0
for i in range(0, s_len):
count = 0
tmp_str = ""
for j in s[i:]:
if j not in tmp_str:
tmp_str += j
count += 1
if count > max_len:
max_len = count
else:
break
return max_len
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: