首页 > 其他 > 详细

LeetCode 3.无重复字符串的最长子串

时间:2020-05-03 00:06:22      阅读:43      评论:0      收藏:0      [点我收藏+]
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        # 字符串str。找出其中不含有重复字符的最长子串的长度。
        
        hash_set = set()
        n = len(s)
        r = 0
        res = 0
        
        
        for i in range(n):    
            if i != 0 :
                hash_set.remove(s[i-1]) # 移除重复的前部元素

                
            while r < n and s[r] not in hash_set: # 移动右指针→,判断s[r]是否重复出现过
                hash_set.add(s[r])                # 没用出现 且 不越界,添加值到哈希集合中
                r += 1                            # 右移指针
                
            res= max(r-i,res)                     # 判取最大值(新的子串距离和原先的子串距离)
                
         
        return res

 

LeetCode 3.无重复字符串的最长子串

原文:https://www.cnblogs.com/sometingintheway/p/12820064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!