Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. Longest Substring Without Repeating Characters #75

Open
tech-cow opened this issue Jan 14, 2020 · 1 comment
Open

3. Longest Substring Without Repeating Characters #75

tech-cow opened this issue Jan 14, 2020 · 1 comment

Comments

@tech-cow
Copy link
Owner

tech-cow commented Jan 14, 2020

globalSet这块看了下答案,没完全写对,再刷一次

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        globalMax = -float('inf')
        globalSet = set()
        j = 0
        
        for i in range(len(s)):
            while j < len(s) and s[j] not in globalSet:
                globalSet.add(s[j])
                globalMax = max(globalMax, len(globalSet))
                j += 1
            globalSet.remove(s[i])
        
        return globalMax if globalMax != -float('inf') else 0
@tech-cow
Copy link
Owner Author

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        maxLen = 0
        visited = set()
        
        n = len(s)
        i, j = 0, 0

        while i < n and j < n:
            if s[j] not in visited:
                visited.add(s[j])
                maxLen = max(maxLen, j - i + 1)
                j += 1
            else:
                visited.remove(s[i])
                i += 1
        return maxLen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant