[LeetCode]题解(python):003-Longest Substring Without Repeating Characters
题目来源:
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的更多相关文章
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
随机推荐
- append与after区别
append() & prepend()实在元素内插入内容(该内容变成该元素的子元素或节点),after() & before()是在元素的外面插入内容(其内容变成元素的兄弟节点).
- C++ 清空消息队列
在button等被禁用后,可能须要它在禁用期间不去响应不论什么消息. 能够使用以下的语句片段: MSG msg; //消耗掉消息队列中的全部消息 while(::PeekMessage(&ms ...
- IOS系列——NStimer
Timer经常使用的一些东西 1. 初始化 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@select ...
- 安卓activity捕获返回button关闭应用的方法
安卓activity捕获返回button关闭应用的方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //按下键盘 ...
- node.js的一些知识
什么是node.js node.js是构建于chrome浏览器v8引擎上的一个js运行环境 可以解析和执行js代码 可以当做另一种上下文,脱离浏览器环境(后端)运行js代码,而代码解析就是基于V8引擎 ...
- JavaScript之向文档中添加元素和内容的方法
一.非DOM方法添加 1.document.write() <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- Java的函数与函数重载
关于Java的函数与函数重载 关于Java的函数与函数重载 1. 函数 假设有一个游戏程序,程序在运行过程中,要不断地发射炮弹.发射炮弹的动作都需要使用一段百行左右的程序代码,在每次发射炮弹的地方都要 ...
- JQuery的Select操作集合
jQuery获取Select选择的Text和Value: 语法解释: $("#select_id").change(function(){//code...}); //为Sel ...
- 【原创】Libjpeg 库使用心得(一) JPEG图像DCT系数的获取和访问
[原创]继续我的项目研究,现在采用Libjpeg库函数来进行处理,看了库函数之后发现C语言被这些人用的太牛了,五体投地啊...废话不多说,下面就进入正题. Libjpeg库在网上下载还是挺方便的,这里 ...
- 浅谈qt 布局器
在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...