题目来源:

https://leetcode.com/problems/longest-substring-without-repeating-characters/


题意分析:

题目是要求出最长的不重复子字符串的长度。比如字符串abcabcbb,得到的最长无重复子字符串就是abc,bca或者cab,那么它最长的不重复长度就是3.


题目思路:

首先,我们可以想到的方法就是每个字符为起点,找到对应的最长不重复子字符串长度,最后进行比较得到最长的不重复子字符串长度。这个方法的时间复杂度为(O(n^2))。如果用这个方法,那么很容易就会TLE。

那么我们回想一下题目例子我们找无重复子字符串的过程。我们从第二个a开始找的时候,找到了倒数第二个b,发现b已经出现过了,这时候,我们再从第二个b开始找,那么得到的无重复子字符串必定比从a开始找要短,那么我们就不需要再从b开始找,而是从c开始找。也就是说,当我们发现这个字符在前面的无重复子字符串出现的位置后一位开始找。如此我们可以节省很多时间,并且我们只需要从头找一次就可以得到答案。时间复杂度为(O(nlogn)。


代码(python):

class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
lls = 1
if len(s) == 0:
return 0
if len(s) == 1:
return 1
i = 1
curbegin = 0
while i < len(s):
cur = s.find(s[i],curbegin,i)
if cur != -1:
lls = max(lls,i - curbegin)
curbegin = cur + 1
i += 1
if s.find(s[len(s) - 1],curbegin,len(s) - 1) == -1:
return max(lls,len(s) - curbegin)
return lls

PS:这里很容易忘记处理当最后一个字符再前面无重复子字符串里面的情况。


转载请注明出处:http://www.cnblogs.com/chruny/

[LeetCode]题解(python):003-Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  2. 【LeetCode】003. Longest Substring Without Repeating Characters

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

  3. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

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

  4. 003 Longest Substring Without Repeating Characters 最长不重复子串

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

  5. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  6. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  7. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  8. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  9. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  10. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

随机推荐

  1. 从零开始学习UNITY3D(GUI篇 GUI.Window)

    unity3d里面,也是包含window窗体的,下面看一下GUI.Window方法的详情 下面我们用代码实现一个通过开关显示窗体的隐藏和显示的功能,代码如下: public class windows ...

  2. 使用window.postMessage实现跨域通信

    JavaScript由于同源策略的限制,跨域通信一直是棘手的问题.当然解决方案也有很多: document.domain+iframe的设置,应用于主域相同而子域不同: 利用iframe和locati ...

  3. jQuery的三种$()

    参考脚本之家“http://www.jb51.net/article/21660.htm”   $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuer ...

  4. C#委托的简单剖析

    为什么在Button1的Click事件发生之后,button1_Click方法就会被调用呢? 实际上,在我们双击Button1的时候,IDE自动的添加了一段代码,该段代码位于“Form1.Design ...

  5. oracle与sql server时间差的取法

    Oracle: oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数*24 为相差的小时数 oracle 两个时间相减默认的是天数*24*60 为相差的分钟数 oracle ...

  6. 多关键字排序(里面有关于操作符(<<运算符 和 >>运算符 )的重载)

    一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...

  7. python 实现单链表

    #! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures ...

  8. Android 电源系列小结s

    package com.ritterliu.newBatteryWidget; import android.app.Activity; import android.app.Service; imp ...

  9. DINOR闪存知识

    闪速存储器(Flash Memory)是一类非易失性存储器NVM(Non-Volatile Memory)即使在供电电源关闭后仍能保持片内信息:而诸如DRAM.SRAM这类易失性存储器,当供电电源关闭 ...

  10. 浅谈 android-query

    介绍:android-query他是在GitHub上的一个开源轻量级的封装库,它集成了网络 .图片加载等模块,可以应用在android中的一些异步应用以及UI的操纵上,通过使用这个框架,使androi ...