码迷,mamicode.com
首页 > 编程语言 > 详细

Leetcode 解题 Longest Substring without repeating charcater python

时间:2015-04-25 22:41:48      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

原题:

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

 

Leetcode 解题 Longest Substring without repeating charcater python

标签:

原文地址:http://www.cnblogs.com/siriuswang/p/4456784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!