LeetCode题解3:Longest Substring Wi
2015-07-01 本文已影响1798人
沙札罕
Longest Substring Without Repeating Characters问题:求给定字符串的最大无重复字符的子串的长度。
难度:中等
思路:用2个变量(start, end)分别保存子串的起点和终点。end自增,直到遇到重复字符为止;从重复字符出现的位置之后1位重新开始扫描。
陷阱:重新开始扫描的位置;循环结束条件
代码:
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
len_max = 0
start = 0
sub_string = ''
for end in xrange(len(s)):
if s[end] not in sub_string:
sub_string += s[end]
else:
len_max = max(len_max, len(sub_string))
while s[start] != s[end]:
start += 1
start += 1
sub_string = s[start : end + 1]
return max(len_max, len(sub_string))
以上解法可求出最大无重复字符的子串本身,由于题目只要求求出最大无重复字符的子串的长度,因此可用Python集合(set)类型略作优化如下:
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
length = 0
char_set = set()
start = end = 0
while start < len(s) and end < len(s):
if s[end] in char_set:
length = max(length, end - start)
char_set.remove(s[start])
start += 1
else:
char_set.add(s[end])
end += 1
return max(length, end - start)
另外,用Python字典(dict)进行上述优化也是可行的,该字典记录了字符到下标的反向索引,代码如下:
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
start = len_max = 0
char_dict = {}
for i in range(len(s)):
if s[i] in char_dict and start <= char_dict[s[i]]:
start = char_dict[s[i]] + 1
else:
len_max = max(len_max, i - start + 1)
char_dict[s[i]] = i
return len_max
最后,使用enumerate和Python字典的get方法可使代码更简洁:
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
max_length, start, char_dict = 0, 0, {}
for idx, char in enumerate(s, 1):
if char_dict.get(char, -1) >= start:
start = char_dict[char]
char_dict[char] = idx
max_length = max(max_length, idx - start)
return max_length