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 ...
随机推荐
- .NET GC简单理解
内存分配 计算对象大小. 添加对象指针和同步索引块. 从内存指针处开始,分配对象内存. 问题:内存不能无限制增长. 垃圾回收 从应用程序实例出发,标记所有的引用对象. 将标记对象移动到低地址端,修正实 ...
- Python学习笔记:读取Excel的xlrd模块
一.安装xlrd 可以使用命令行安装也可使用pycharm进行安装 表示xlrd库已经安装成功,安装成功后,我们就可以导入使用了. 二.xlrd说明 (1.单元格常用的数据类型包括 0:empty(空 ...
- Linux安装Vmware Tools/vmtools(通用)
以下方法适用于centos/Ubuntu #新建一个临时目录,用于挂载光驱 mkdir /tmp/cdrom mount -t iso9660 /dev/cdrom /tmp/cdrom cp -r ...
- Linux系统格式化命令mke2fs命令简析
1.mke2fs配置文件: # vim /etc/mke2fs.conf [defaults] base_features = sparse_super,filetype,resize_inode,d ...
- 基于nodeJS的小说爬虫实战
背景与需求分析 最近迷恋于王者荣耀.斗鱼直播与B站吃播视频,中毒太深,下班之后无心看书. 为了摆脱现状,能习惯看书,我开始看小说了,然而小说网站广告多而烦,屌丝心态不愿充钱,于是想到了爬虫. 功能分析 ...
- Unity3D入门 UnityAPI常用方法和类
时间函数: 这里只列举了一部分,更多的看Scripting API using System.Collections; using System.Collections.Generic; using ...
- AE调用GP工具(创建缓冲区和相交为例)
引用 Geoprocessing是ArcGIS提供的一个非常实用的工具,借由Geoprocessing工具可以方便的调用ArcToolBox中提供的各类工具,本文在ArcEngine9.2平台环境下总 ...
- Cisco的动态Nat、PAT
Lab_C(config)# interface Ethernet0/0 ip address 192.168.30.2 255.255.255.0 router rip network 172. ...
- Misc题目
@freebuff教程https://www.freebuf.com/column/196815.html @巅峰极客wp https://www.anquanke.com/post/id/18914 ...
- 华为HCNA乱学Round 7:VLAN间路由