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的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- Ajax 实现无刷新页面
注意:如本文所用,在前面的文章库的数目可以在源代码中找到,我将指示在文本,其中链路,为了缩短制品的长度,阅读由此带来的不便.乞求被原谅. 评论文章 Ajax 实现无刷新页面.其原理.代码库.代码. 这 ...
- HDU1068/POJ1466_Girls and Boys(二分图/最大独立集=N-最大匹配)
解题报告 http://blog.csdn.net/juncoder/article/details/38160591 题目传送门(POJ) 题目传送门(HDU) 题意: 求满足条件的最大集合:集合内 ...
- Session or Cookie?是否有必要使用Tomcat等一下Web集装箱Session
Cookie是HTTP协议标准下的存储用户信息的工具.浏览器把用户信息存放到本地的文本文件里. Session是基于Cookie实现的. 2011年4月,武汉群硕面试的时候(实习生).面试官也问过这个 ...
- Sort函数的相关知识
sort与stable_sort 需包含头文件:#include <algorithm>因为它是库函数 这两个函数的原理都是快速排序,时间复杂度在所有排序中最低,为O(nlog2n) ...
- C#将Excel数据导入数据库(MySQL或Sql Server)
最近一直很忙,很久没写博客了.今天给大家讲解一下如何用C#将Excel数据导入Excel,同时在文章最后附上如何用sqlserver和mysql工具导入数据. 导入过程大致分为两步: 1.将excel ...
- Mac+PhpStorm+XAMPP+Xdebug
Mac+PhpStorm+XAMPP+Xdebug 环境的配置 在上一篇 PHP 系列的文章<PHP 集成开发环境比较>中,我根据自己的亲身体验,非常简略的介绍和对比了几款常用的集成开 ...
- 解决tomcat占用8080端口
怎么解决tomcat占用8080端口问题图文教程 怎么解决tomcat占用8080端口问题 相信很多朋友都遇到过这样的问题吧,tomcat死机了,重启eclipse之后,发现 Se ...
- Reactive Extensions(Rx)并发浅析
Reactive Extensions(Rx)并发浅析 iSun Design & Code .Net并行编程 - Reactive Extensions(Rx)并发浅析 关于Reactive ...
- 加快XCode编译链接速度(200%+)—XCode编译慢液
最近在一个大型项目的开发的时候遇到一个很头疼的问题,由于该项目的代码更,每次建立联系1纪要.浪费时间调试.因此,一些研究如何提高编译链接速度,这里给大家分享. 为了提高编译和链接的是以下三种方式的速度 ...
- passenger安装nginx
1.更换淘宝gem gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/ 2.gem安装 ...