LeetCode OJ -- 无重复字符的最长子串
给定一个字符串,找出不含有重复字符的 最长子串 的长度。
示例:
给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3。
给定 "bbbbb" ,最长的子串就是 "b" ,长度是1。
给定 "pwwkew" ,最长子串是 "wke" ,长度是3。请注意答案必须是一个子串,"pwke" 是 子序列 而不是子串。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
/* 实现方式采用了一遍过的方式,将所有字符和最近出现位置存储于map中,
map查找效率O( log(n) )。遍历一次O(n) ,效率为O( nlog(n) ) */
int i = ;
map<char, int> mp;
int Max = ; //表示最大子串大小
int nBegin = ; //最大子串的开始位置
int nEnd = ; //最大子串的结束位置
int bRepet = false; //判断字符串是否开始出现重复字符
int nLastRptA1 = ; //当前重复字符倒数第二次出现的位置
int nLastRptA2 = ; //当前重复字符出现的位置
for (; i<s.size(); i++)
{
char tmp = s[i];
map<char, int>::iterator mapIte = mp.find(tmp); //
if (mapIte == mp.end()) //第一种情况
{
mp.insert(pair<char, int>(tmp, i));
if (!bRepet)
{
Max++;
nEnd = i;
}
else
{ //第二种情况
int LastRtpDur = nLastRptA2 - nLastRptA1;
int nNewMax = LastRtpDur + i - nLastRptA2;
Max = (nNewMax > Max) ? nNewMax : Max;
nBegin = nLastRptA1 + ;
nEnd = i;
}
}
else
{
bRepet = true;
if (mapIte->second >= nLastRptA1) //第三种情况
{
int tmp = i - mapIte->second;
Max = (tmp > Max) ? tmp : Max;
if (tmp > Max)
{
nBegin = mapIte->second + ;
nEnd = i;
}
nLastRptA1 = mapIte->second;
nLastRptA2 = i;
mapIte->second = i;
}
else { //第四种情况
int tmp = i - nLastRptA1;
Max = (tmp > Max) ? tmp : Max;
if (tmp > Max)
{
nBegin = nLastRptA1 + ;
nEnd = i;
}
//nLastRptA1 = mapIte->second;
// nLastRptA2 = i;
mapIte->second = i;
}
}
}
return Max;
}
};
983个测试用例用时:20ms
下面是用时最短(16ms)的代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int length=s.size();
int pre = -;
int m[];
memset(m,-,*sizeof(int));
int maxlength = ;
//int count=0;
for(int i=;i<length;i++)
{
pre = max(pre,m[s[i]]);
maxlength = max(maxlength,i-pre);
m[s[i]]=i;
}
return maxlength;
}
};
LeetCode OJ -- 无重复字符的最长子串的更多相关文章
- Leetcode(三)无重复字符的最长子串
3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...
- 【LeetCode】无重复字符的最长子串【滑动窗口法】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- [LeetCode] 3. 无重复字符的最长子串
题目链接:(https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) 题目描述: 给定一个字符 ...
- 【leetcode 3. 无重复字符的最长子串】解题报告
思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- Leetcode——3. 无重复字符的最长子串
难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一 ...
- 力扣Leetcode 3. 无重复字符的最长子串
无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...
- [LeetCode]3. 无重复字符的最长子串(滑动窗口)
题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...
- [LeetCode]3.无重复字符的最长子串(Java)
原题地址: longest-substring-without-repeating-characters/submissions 题目描述: 示例 1: 输入: s = "pwwkew&qu ...
随机推荐
- ScrollView设置了ContentSize高度为0,仍然能滑动的问题
你有没有遇到过这样的情况: 对于ScrollView的不能上下滑动,设置了以下代码: _scrollViewTitle=[[UIScrollView alloc]initWithFrame:CGRec ...
- class文件访问标志
class文件中用两个字节共16位代表访问标志(access flags),用于表明该类或接口被访问时能提供的一些信息: 标志名称 标志值 含义 ACC_PUBLIC 0x00 01 是否为Publi ...
- 开发单体web shop] 6. 商品分类和轮播广告展示
目录 商品分类&轮播广告 商品分类|ProductCategory 需求分析 开发梳理 编码实现 轮播广告|SlideAD 需求分析 开发梳理 编码实现 福利讲解 源码下载 下节预告 商品分类 ...
- 移动端1px 边框
伪类+ transform .border_1px:before{ content: ''; position: absolute; top: 0; height: 1px; width: 100%; ...
- PHP 生成器 yield理解
如果是做Python或者其他语言的小伙伴,对于生成器应该不陌生.但很多PHP开发者或许都不知道生成器这个功能,可能是因为生成器是PHP 5.5.0才引入的功能,也可以是生成器作用不是很明显.但是,生成 ...
- EL表达式的11隐含对象
EL表达式在不同范围如何取值: <% pageContext.setAttribute("book", "红楼梦"); request.setAttrib ...
- Java拆箱装箱
原文 http://www.cnblogs.com/dolphin0520/p/3780005.html
- RS232与RS485的功能与区别!
转载于:http://blog.csdn.net/kevinhg/article/details/7367144 RS232接口是1970年由美国电子工业协会(EIA)联合贝尔系统.调制解调器厂家及计 ...
- 算法 - k-means算法
一.聚类思想 所谓聚类算法是指将一堆没有标签的数据自动划分成几类的方法,属于无监督学习方法,这个方法要保证同一类的数据有相似的特征,如下图所示: 根据样本之间的距离或者说是相似性(亲疏性),把 ...
- EXKMP模版
这道题目折腾了我好一会啊,出于尊重我要先放我们师兄的博客 1178: [视频]EXKMP模版:最长共同前缀长度 时间限制: 1 Sec 内存限制: 128 MB提交: 180 解决: 123[提交 ...