[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 ...
随机推荐
- Lua学习2 Lua小框架的搭建
看了上一篇Lua环境搭建具体http://blog.csdn.net/liuwumiyuhuiping/article/details/9196435 为了方便学习. 具体新开始搭建一下学习的小框架. ...
- 从零开始学习UNITY3D(GUI篇 2)
复合控件极其使用,toolbar,selectgrid 先看效果图: toolbar可以看作是一个button按钮的集合,一次只能点击一个按钮, selectgrid又可以堪称是一个toolbar的集 ...
- 提交服务器 post get
HttpRequest Post or Get // method --- WebRequestMethods.Http.Post 或 WebRequestMethods.Http.Get priva ...
- iOS实践01
去年放假之前大概完成了新浪微博项目,到现在也忘得差不多了,打算在重新写一遍.之前的一些笔记在新浪的博客SleenXiu,在这主要是把新浪微博以随笔的形式写在这,方便以后的复习. 先看看之前主要完成的几 ...
- ROS中编辑文件命令行工具rosed
rosed是rosbash套件中的一个,它允许我们通过包名直接编辑包中的文件,而不是输入包的全部路径. 用法: rosed [package_name] [filename] 例如: rosed ro ...
- Hadoop经典面试题(转)
单项选择题 1. 下面哪个程序负责 HDFS 数据存储. a)NameNode b)Jobtracker c)Datanode d)secondaryNameNode e)tasktracker 2. ...
- MVC打开电脑对话框
//下载文件 public ActionResult Download(int id) { //依靠模板生成文档 var path =要下载的文件的路径 var name = Path.GetFile ...
- codevs 1455 路径 计算m^n%p
题目链接 题目描述 Description 小明从A1到An+1,他知道从A1到A2,从A2到A3,......,从An到An+1都有m条路,且从A1到An+1都只有这些路.小明想知道,从A1地到An ...
- codeforces 401D. Roman and Numbers 数位dp
题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. #include<bits/stdc++. ...
- Web开发者需具备的8个好习惯
优秀的Web开发人员工作效率更高,因为他们拥有丰富的经验和良好的习惯.作者Gregor Dorfbauer分享了用于Web开发中的8个好习惯,这些良好的工作习惯不仅能提高效率,还能让您创建更加优秀的应 ...