leetcode题解3-Longest Substring Without Repeating Characters

标签: 动态规划
3人阅读 评论(0) 收藏 举报
分类:

题目:

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 subsequence and not a substring.


思路:动态规划寻找最长不重复子串。

代码:

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        max_temp,max_res,temp_s = 0, 0, ''
        for c in s:
            if c in temp_s:
                temp_s = temp_s[temp_s.index(c)+1::] + c
                if max_temp >= max_res:
                    max_res = max_temp
                max_temp = len(temp_s)                     
            else:
                temp_s += c
                max_temp += 1
        return max(max_temp, max_res)

查看评论

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
  • 3742

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
  • 698

[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
  • 6029

LeetCode(3) Longest Substring Without Repeating Characters

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

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
  • 1337

LeetCode--longest-substring-without-repeating-characters

题目描述 Given a string, find the length of the longest substring without repeating charact...
  • wl_ss
  • wl_ss
  • 2018-01-19 22:53:48
  • 80

leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)

leetcode第三题,自己的算法,不知道复杂度怎样,希望能与网友交流。
  • hellowangxyue
  • hellowangxyue
  • 2016-04-05 09:47:32
  • 629

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
  • 19153

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
  • 8090

leetcode python - Longest Substring Without Repeating Characters

# Given a string, find the length of the longest substring without repeating characters. # # Example...
  • AMDS123
  • AMDS123
  • 2017-04-01 15:07:19
  • 6483
    个人资料
    持之以恒
    等级:
    访问量: 1370
    积分: 201
    排名: 36万+
    文章存档
    最新评论