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的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- ASP.NET程序读取二代身份证(附源码)
原文:ASP.NET程序读取二代身份证(附源码) 一般来说winform应用程序解决这个问题起来时很容易的,web应用程序就麻烦一点了. 这里我说说我的解决思路: 一.你必要有联机型居民身份证阅读器一 ...
- MySQL之终端(Terminal)管理MySQL
原文:MySQL之终端(Terminal)管理MySQL 前言:MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”. 现在我写MySQL的终端命令操作 ...
- 学习笔记 broswerify + watchify + beefy
broswerify “Browserify lets you require('modules') in the browser by bundling up all of your depende ...
- Kinect的学习笔记发展一Kinect引进和应用
Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.Kinect简单介绍 Kinectfor Xbox ...
- js 通信
js 页面间的通信 看了一下公司原来的代码,原页面ajax post返回一个页面完整的HTML,然后再打开一个新页面并输出ajax返回的所有代码到新页面上,在新页面上以表单提交的形式实现重定向. 任凭 ...
- PHP从零单排(十八)图像处理
1.打开现有的图像 <?php header("Content-type:image/jpeg"); $img=imagecreatefromjpeg("cc.jp ...
- java流下载
@RequestMapping("/pluginDownload") public void pluginDownload(HttpServletResponse response ...
- javascript系列之变量对象
原文:javascript系列之变量对象 引言 一般在编程的时候,我们会定义函数和变量来成功的构造我们的系统.但是解析器该如何找到这些数据(函数,变量)呢?当我们引用需要的对象时,又发生了什么了? 很 ...
- 在vc正在使用xtremetoolkit接口库-----使用简单的控制
首先,我们需要在StdAfx.h增加头文件: #include "XTToolkitPro.h" #include "XTPResource.h" 在test. ...
- [置顶] Spring中DI设置器注入
Java的反射机制可以说是在Spring中发挥的淋漓尽致,下面要看的代码就是通过反射机制来实现向一个类注入其实际依赖的类型,这个过程的实现会交由Spring容器来帮我们完成. JavaBean中针对属 ...