leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
意思是给定字符串s, 返回最长的没有重复字符的字符串的长度。例如“abcddd”就是“abcd”长度为4,这题我自己试了试发现不是错误就是超时。最终还是不得不参考了其他大神。代码如下:
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int start = ;
int max = ;
int len = s.length();
if( len == )
return ;
if (len ==)
return ; for (int i = ; i < len; i++)
{
for (int j = i - ; j >= start; j--) // 注意了是 >= 忘记=就错了
{
if (s[i] == s[j])
{
start = j + ;
break;
}
else
{
if (max < i - j + )
max = i - j + ;
}
}
}
return max;
}
};
如果输入的字符串为空后者为一,可以直接返回长度零或者1。
如果输入的长度大于1,那么我们就可以从下标1个开始(因为下标是从零开始的,所以下标1其实是第二个)
每次都只需要判断当前往回回溯到start点有没有重复字符。注意了这里的start不总是指第一个字符,而是根据相同字符在变。
举个例子吧:abcabcdf
第一次的时候当前的为第二个字符,也就是b,这时前面只有一个a,没有重复,所以记录i-j+1为2到max中,同样c的时候max就是3了,再到第二个a的时候,我们可以注意到现在的最大长度已经在刚才c的时候记录了。这时因为重复了这是第一次发现有重复的字符,所以我们的答案中肯定不可能包含这两个重复字符(因为题目要求就是不重复)。那么由于前面的可能的最大字符已经记录在max了。我们只需把start点定位到第一个重复字符的下一位,此例中是b。
同样在继续当前已经到第二个b了发现重复,再把start点定位到c,继续,当前到第二个c,又重复,则start定位到a,当前点到d,此时i-j+1是4,更新max,最后当前到f,max更新为5
所以此例最终答案为5.就是这样了。
leetcode第三题--Longest Substring Without Repeating Characters的更多相关文章
- 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(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 刷题之路第三题--Longest Substring Without Repeating Characters
问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...
- 【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. Examples: Giv ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- 开源GUI-Microwindows之程序入口分析
**************************************************************************************************** ...
- 辛星整理3linux笔记,免费下载点,我希望对你有所帮助
忙乱,这是我第一次看李指出老师的视频时,,这本书是关于116页面,在csdn下载对:点我下载 ,假设左边的地址崩溃了,也能够在浏览器中输入例如以下地址然后下载:http://download.csdn ...
- 使用python+flask让你自己api(教程源代码)
1.背景 ok,这可能是很多朋友和我一样经常使用的各种api,例facebook的.github的.甚至微信api.因此,很多人都想使自己的api.在线教程在这方面它是非常小的,今天,我做了一个平稳, ...
- android做设计的每一个屏幕尺寸和分辨率(一个)
一个.与分辨率无关 1.使用dp(dpi) Android密度不依赖像素(dp)指定屏幕尺寸,它同意不同的屏幕尺寸和像素密度类似设备通过缩放来达到同样的效果. (不解决不同屏幕尺寸的问题?) 2.的资 ...
- DTD验证XML(转)
1.内部DTD 最简单的使用DTD的方法是在XML文件的序言部分加入一个DTD描述,加入的位置是紧接在XML处理指示之后.一个包含DTD的XML文件的结构为: <?xml ve ...
- javaEE异常摘要——更换工作区相同tomcat当部署在同一个项目疑难解答
我有一个项目,我的工作区公告,没问题,它可以运行正常,但我把项目copy还有一个工作空间,然后发布到tomcat(随着tomcat,先前的工作空间remove deployment,公布信息)上去,想 ...
- 设置韩澳大利亚sinox弄winxp清除字体和界面美观
澳大利亚开始与汉sinox一直以为接口暗淡,字体比较模糊,否winxp光明,导致眼比较辛苦的眼睛.比方说,可能不那么黯淡刺眼,有益眼睛,但我不能忍受字体模糊.即使调整分辨率,,但是字体模糊还是没有改观 ...
- SQL于DML(数据库操作语言)采用
1.Insert语句: INSERT [INTO] table [(column1, column2, column3, . . .)] VALUES(value1, value2, value3, ...
- C#关于HttpClient的应用(二):融云IM集成
public class RcHttpClient:BaseHttpClient { private String appKey; private String appSecret; public R ...
- Android UI开发神兵利器之Android Asset Studio
Android Asset Studio 这个工具提供了一系列的资源设计实现,很推荐: