无重复字符的最长子串【滑动窗口】【哈希】

发布时间:2024年01月06日

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

思路 & 解题方法

滑动窗口经典题目

复杂度

时间复杂度:

添加时间复杂度, 示例: O ( n ) O(n) O(n)

空间复杂度:

添加空间复杂度, 示例: O ( n ) O(n) O(n)

Code

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        ans = 0
        left = 0
        length = len(s)
        dic = set()
        for to in range(length):
            if s[to] in dic:
                ans = max(ans, to - left)
                while s[left] != s[to]:
                    dic.remove(s[left])
                    left += 1
                left += 1
            else:
                dic.add(s[to])
        ans = max(ans, length - left);
        return ans
文章来源:https://blog.csdn.net/qq_45985728/article/details/135426026
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。