关闭

[leetcode] Longest Substring Without Repeating Characters

标签: leetcode
249人阅读 评论(0) 收藏 举报
分类:

Q: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

解题思路:动态规划

class Solution:
    # @param {string} s
    # @return {integer}
     def lengthOfLongestSubstring(self, s):
        if s is None or len(s)==0:
            return 0
        if len(s)==1:
            return 1
        dic = {}
        maxcount,startindex = 0,-1
        for index,i in enumerate(s):
            if i in dic and startindex<dic[i]:
                startindex = dic[i]
            if index-startindex>maxcount:
                maxcount = index-startindex
            dic[i]=index
        return maxcount
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55888次
    • 积分:1032
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:16篇
    • 译文:1篇
    • 评论:2条
    Github
    文章分类
    最新评论