[算法] 刷题-无重复最长子串
1. 双指针滑窗 相对于暴力法,需要优化搜索空间,滑窗的目的是候选子串的长度至少不少于最长子串的长度,再通过当前子串提供的信息,进行优化 常规 思路很简单,使用beg, end两个指针记录当前位置,用is_unique 判定是否为重复子串,如果不是且长度增加则更新end+1,如果是,则滑动窗口+1 def is_unique(counts, s): counts = dict() for k, v in enumerate(s): if v in counts: return False else: counts[v] = True return True, 1 def lengthOfLongestSubString(s): if s is None: return 0 if len(s)<2: return len(s) counts = dict() beg = dict() end = 1 max_sub = '' search = 0 while end <= len(s): sub = s[beg:end] unique = is_unique(counts, sub, search) if unique and len(sub) > len(max_sub): max_sub = sub end += 1 if not unique: beg += 1 end +=