「LeetCode」0002-Longest Substring Without Repeating Characters(C++)
分析
贪心思想。注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车)。
代码
关掉流同步能有效提高速度。
static const auto io_sync_off = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
array<int, 256> m;
m.fill(-1);
int maxlen=0, l=0, idx=0;
for(auto c:s)
{
l=max(l,m[c]+1);
maxlen=max(maxlen,idx-l+1);
m[c]=idx++;
}
return maxlen;
}
};
改进的时间变化:24ms->20ms->8ms
「LeetCode」0002-Longest Substring Without Repeating Characters(C++)的更多相关文章
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)
通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 安卓渗透测试工具Drozer学习笔记
下载,并安装. pip安装即可,安装完成后可能会出现缺少twisted依赖库的问题 ➜ vul git:(master) ✗ drozer console connect drozer Server ...
- Entity Framework——性能测试
内容提要 一.对EF框架的性能测试 增.删.改,查测试及性能优化 二.使用sql执行 增.删.改,查测试 三.对以上两种方式对比分析 一 对EF框架的测试 1插入操作测试 测试代码(关键部分) Lis ...
- Vue.js-简单的增删查功能
1.Vue.js是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图 ...
- 【绝迹篇】RSA加密算法(私钥加签公钥验签)
对于上上篇博客中我讲的一个故事,本文引用: https://www.cnblogs.com/ButterflyEffect/p/9851403.html 故事中提到的关于加密会出现,私钥加密,公钥解密 ...
- springboot项目pom添加依赖
在dependency 后面 ALt+/ 可以打开编辑窗口.
- C/C++ Windows API——获取计算机信息 转
转自:http://blog.csdn.net/chy555chy/article 函数 头文件 作用 GetVersionEx <windows.h> 获取系统版本信息(deprecat ...
- UE4 Navmesh 室内导航设置
我用的UE版本是4.14.1 系统:win10 64 前不久给样板房里面做了一个扫地机器人,导航设置让我头大了很久,度娘也没有用,最后在谷哥上有所感悟,现在给出本人的设置过程和解决方案. 一开始拖 ...
- MySQL语句整理(二)
数据库操作前的准备 -- 创建数据库 -- create database python_test_1 charset=utf8; -- 使用数据库 -- use python_test_1; -- ...
- Spring的扩展
Spring中引用属性文件 JNDI数据源 Spring中Bean的作用域 Spring自动装配 缺点
- spring-quartz 定时器 给targetMethod传递参数
今天在做一个项目的时候,要给一个定时器任务的执行方法传递参数,在网上找了一下资料,可以使用arguments参数: <bean id="subsidyJobDetail" ...