3. Longest Substring Without Repeating Characters

原创 2018年04月15日 11:03:23

3Longest Substring Without Repeating Characters

题目描述

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequenceand not a substring.

分析

只需要返回最大长度值,所以只需要一个int型变量保存返回结果;

定义两个指针,记录当前找到的无重复子串的起始和终止位置(终止即当前遍历到的索引位置)。

完整代码

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        maxlen=start=0
        used={}        #保存已遍历的字符和最近出现的位置
        for i,c in enumerate(s):
            if c in used and start<=used[c]:    #若当前字符之前出现过,并且在start之后
                start=used[c]+1     #更新起点
            else:   
                maxlen=max(maxlen,i-start+1)    #更新最大长度
            used[c]=i   #更新字符出现的位置
        return maxlen

【Python】第三周 Python Web开发基础(上)

-
  • 1970年01月01日 08:00

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-27 12:55:12
  • 6039

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUnique...
  • feliciafay
  • feliciafay
  • 2013-11-23 04:25:39
  • 17069

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, ...
  • dragon_dream
  • dragon_dream
  • 2014-02-18 22:22:49
  • 4743

leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, ...
  • wangyaninglm
  • wangyaninglm
  • 2016-04-05 22:27:27
  • 3779

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在Leet...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 01:53:37
  • 19167

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: Given a string, find the length of the longest substring without repeating characters. Exa...
  • banana1006034246
  • banana1006034246
  • 2017-07-15 17:35:47
  • 709

字符串+哈希表+小动态规划(Longest Substring Without Repeating Characters -- LeetCode)

Longest Substring Without Repeating Characters 题目难度:3   面试频率 2  . (1-5) 题目描述: Given a string, fin...
  • CSDNyuanyuan
  • CSDNyuanyuan
  • 2016-09-03 12:16:17
  • 363

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a str...
  • j754379117
  • j754379117
  • 2016-06-20 20:36:28
  • 1355

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For examp...
  • hu1020935219
  • hu1020935219
  • 2014-08-03 20:38:44
  • 8099
收藏助手
不良信息举报
您举报文章:3. Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)