「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 ...
随机推荐
- (第二场)D Money 【dp\贪心】
题目:https://www.nowcoder.com/acm/contest/140/D 题目描述: White Cloud has built n stores numbered from 1 t ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室 实战系列(内容已过期,阅读请慎重)
项目简介 利用ASP.NET SignalR技术与Layim前端im框架实现的一个简单的web聊天室,包括单聊,群聊,加好友,加群,好友搜索,管理,群组管理,好友权限设置等功能.涉及技术: Elast ...
- Sublime 快捷键一览表
Sublime Text 快捷键一览表 Sublime Text比起Notepad++,UltraEdit之类Code编辑器来说,其功能有过之而无不及,配合着各种插件使用,Sublime Text在实 ...
- Selenium图片上传
方式1: 如果是input类型的标签则可直接赋值 部分代码: driver.find_element_by_name("file").send_keys("E:\\tes ...
- Gradle Goodness: Profiling Information
If we want to know more about how much time is spent in tasks we can use the --profile command-line ...
- Vue多个元素展开收起
html: <div class="helpPages_main"> <div class="read" v-for="(item, ...
- Python 学习笔记(十一)Python语句(一)
运算符和条件语句 算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...
- window下安装composer
1.什么是composer 一个智能的下载工具.比如说我的项目要安装yii框架,而yii是依赖于其他东西的,仅仅安装yii是不够的,这样会导致我的项目也不能正常运行:怎么办呢,我们可以一个一个手动的将 ...
- Undefined symbols for architecture arm64: "_OBJC_CLASS_$XXX", referenced from: objc-class-ref in XXX
ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 ...
- iOS之webview加载网页、文件、html的方法
UIWebView 是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf.word.doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过a ...