• Leetcode 解题 Longest Substring without repeating charcater python


    原题:

    Given a string, find the length of the longest substring without repeating character

    For example, the Longest substring without repeating letters for "abcabcbb" is "abc", with the length is 3

    思路:参考blog.csdn.net/hcbbt/article/details/43966513

           开一个数组记录当前字符最近出现的位置,遍历,更新左边界,用它计算最大值。

    代码:

    class Solution:
        def lenthOfLongestSubstring(self, s):
            res = 0
            left = 0
            d = {}
    
            for i , ch in enumerate(s):
                if ch in d and d[ch] >= left: left = d[ch] + 1
                d[ch] = i
                res = max(res, i -left + 1)
            return res
    
  • 相关阅读:
    毕设进度28
    任务27
    任务26
    任务25
    任务24
    第二次冲刺
    课堂作业-搜狗输入法
    课堂作业-寻找水王
    博客花园典型用户和场景
    第十天
  • 原文地址:https://www.cnblogs.com/siriuswang/p/4456784.html
Copyright © 2020-2023  润新知