[LintCode] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for"abcabcbb"
is "abc"
, which the length is 3
.
For "bbbbb"
the longest substring is "b"
, with the length of 1
.
O(n) time
LeetCode上的原题,请参见我之前的博客Longest Substring Without Repeating Characters。
解法一:
class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string s) {
int res = , left = -;
vector<int> m(, -);
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string s) {
int res = , left = , right = ;
unordered_set<char> st;
while (right < s.size()) {
if (!st.count(s[right])) {
st.insert(s[right++]);
res = max(res, (int)st.size());
} else {
st.erase(s[left++]);
}
}
return res;
}
};
[LintCode] Longest Substring Without Repeating Characters的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- WebRTC之带宽控制部分学习(1) ------基本demo的介绍
转自:http://blog.csdn.net/u013160228/article/details/46392037 WebRTC的代码真是非常之大啊,下载以及编译了我好几天才搞完..... 可以看 ...
- Hbuilder连接模拟器调试
Hbuilder是一个非常好用的HTML5开发IDE,我最喜欢的功能就是连接手机调试了,连接手机调试有两种途径,一是通过USB连接真机,二是下载安装一个安卓模拟器,让Hbuilder连接到安卓模拟器, ...
- list[C++]
//双向链表 #include <iostream> using namespace std; #include <list> int main(int argc, const ...
- Windows系统上安装多个版本jdk,修改环境变量不生效
本机已经安装了jdk1.6,而比较早期的项目需要依赖jdk1.5,于是同时在本机安装了jdk1.5和jdk1.6. 安装jdk1.5前,执行 java -version 得到java version ...
- 映射一对多双向关联关系 cascade、inverse、属性
当类与类之间建立了关联,就可以方便的从一个对象导航到另一个对象.或者通过集合导航到一组对象.例如: 对于给定的Emp对象,如果想获得与它关联的Dept对象,只要调用如下方法 Dept dept=emp ...
- DOM、Window对象操作
一.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 一.基本语法: 数据类型(字符串,小数,整数,布尔,时间) var, v ...
- flex弹性布局
Flex 布局教程:语法篇 原文地址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 作者: ...
- 对NLP的一些新认识
其实这是老板让上交的一份总结,贴出来,欢迎朋友们批评指正. 最近看了一部分关于NLP的几篇论文,其中大部分为神经网络实现, 从基本的HMM算法实现,到LSTM实现,有很多方法可以用来处理NLP任务中的 ...
- 破解ZIP加密文件密码fcrackzip
破解ZIP加密文件密码fcrackzip ZIP是最常见的文件压缩方式.由于其压缩算法开源,主流操作系统都支持这种压缩算法.ZIP压缩方式支持密码加密.加密的时候会在文件头部保存密钥相关信息.利用这个 ...
- JS(event事件)
常用的event事件: 属性 此事件发生在何时... onabort 图像的加载被中断. onblur 元素失去焦点. onchange 域的内容被改变. onclick 当用户点击某个对象时调用的事 ...