分析

贪心思想。注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车)。

代码

关掉流同步能有效提高速度。

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++)的更多相关文章

  1. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

    通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...

  4. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  5. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  6. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  7. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  8. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  9. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  10. 【LeetCode】3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. 刷题防止Time Limit Exceeded(TLE)技巧

    1.C++ 不要使用cin,cout,该使用scanf和printf 2.Java 不要使用Scanner,改用BufferedReader 3.Python 在文件开始的地方加入 import ps ...

  2. 机器学习之感知器算法原理和Python实现

    (1)感知器模型 感知器模型包含多个输入节点:X0-Xn,权重矩阵W0-Wn(其中X0和W0代表的偏置因子,一般X0=1,图中X0处应该是Xn)一个输出节点O,激活函数是sign函数. (2)感知器学 ...

  3. 关于最新版AFNetworking(3.0)上传多张图片的问题

    最新版的AF已经废弃了很多以前的类,所以很多以前的方法都不能用了,当然最主要还是为了适应ipV6所做的更改.楼主最近正在写多张图片上传,碰到了一些问题,解决之后直接封装了一个方法,废话有点多了,上代码 ...

  4. 安装sqlserver2016出现的错误

    今天安装sharepoint 2016,projectserver 2016,在安装sqlserver2016的时候突然报错了,提示需要安装oracle java se, Rule “Oracle J ...

  5. AFNetworking 打印错误信息(二进制信息)

    AFNetworking 打印错误信息(二进制信息) NSError *underError = error.userInfo[@"NSUnderlyingError"]; NSD ...

  6. 『C++』Temp_2018_12_06

    #include <iostream> #include <string> using namespace std; class Type{ public: string Na ...

  7. 断言assert()与调试帮助

    列表内容assert()是一种预处理宏(preprocessor marco),使用一个表达式来作为条件,只在DEBUG模式下才有用. assert(expr); 对expr求值,如果expr为假,则 ...

  8. 字符编码ascii、unicode、utf-­‐8、gbk 的关系

    ASIIC码: 计算机是美国人发明和最早使用的,他们为了解决计算机处理字符串的问题,就将数字字母和一些常用的符号做成了一套编码,这个编码就是ASIIC码.ASIIC码包括数字大小写字母和常用符号,一共 ...

  9. 08.nextcloud搭建

    由于公司用的nfs文件共享系统满足不了权限需求,测试nextcloud是否符合要求 参考博客: https://www.cnblogs.com/davidz/articles/9686716.html ...

  10. npm安装包时 --save 和 --save-dev 的区别

    以npm 安装 vue为例 1.npm install vue: 会把vue包安装到node_modules目录中: 不会修改package.json文件: 之后运行npm install命令时,不会 ...