Skip to content

3. Longest Substring Without Repeating Characters #75

New issue

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

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

Already on GitHub? # to your account

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

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

No branches or pull requests

1 participant