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 ...
随机推荐
- openstack部署keystone
环境: 免密钥,域名解析 cat /etc/hosts 192.168.42.120 controller 192.168.42.121 compute 192.168.42.122 storage ...
- linux常用命令(14)which命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数 ...
- 队列:Beanstalkd介绍
一:介绍 Beanstalkd 是一个轻量级的内存型队列.它是典型的类Memcached设计,协议和使用方式都是同样风格.github:https://github.com/beanstalkd官网: ...
- property自己实现
# 先回顾一下 class Room: def __init__(self,name,width,length): self.name = name self.width = width self.l ...
- linux下Eclipse进行C编程时动态链接库的生成和使用
引用 http://linux.chinaitlab.com/soft/864157.html 欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 一.创建动态链接库1.创建 ...
- elk 概念整理 集群状态 - yellow - 面试的问题 -- 官方配置文档 水平扩容以及数据保障
1. primary shard -- raid0 2.replicas shard -- raid1 3.index -- 图书馆的借书指引 4.MySQL vs elasticsearch # ...
- Js 执行上下文和作用域
1.执行上下文和执行栈 执行上下文就是当前 JavaScript 代码被解析和执行时所在环境的抽象概念, JavaScript 中运行任何的代码都是在执行上下文中运行. 执行上下文的生命周期包括三个阶 ...
- 10分钟学会web通讯的四种方式,短轮询、长轮询(comet)、长连接(SSE)、WebSocket
一般看到标题我们一般会产生下面几个问题??? 什么是短轮询? 什么是长轮询? 长连接又是什么? wensocket怎么实现呢? 他们都能实现web通讯,区别在哪呢,哪个好用呢? 接下来我们就一个个来了 ...
- 2019牛客暑期多校训练营(第一场)-E(DP)
题目链接:https://ac.nowcoder.com/acm/contest/881/E 题意:求可分解成n个AB和m个BA的字符串的个数. 思路: 首先根据贪心思想,前n个A可作为AB的A,后m ...
- PTA(Basic Level)1021.个位数统计
给定一个 k 位整数 \(N=d_{k−1}10^{k−1}+⋯+d_110^1+d_0 (0≤d_i≤9, i=0,⋯,k−1, d_{k−1}>0)\),请编写程序统计每种不同的个位数字出现 ...