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

Have you met this question in a real interview?

Yes
Example

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.

Challenge

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

  1. LeetCode[3] Longest Substring Without Repeating Characters

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

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  3. Longest Substring Without Repeating Characters

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

  4. 3. Longest Substring Without Repeating Characters(c++) 15ms

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

  5. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  6. Longest Substring Without Repeating Characters(C语言实现)

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

  7. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  8. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  9. Longest Substring Without Repeating Characters (c#)

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

随机推荐

  1. Linux命令之ar - 创建静态库.a文件和动态库.so

    转自:http://blog.csdn.net/eastonwoo/article/details/8241693 用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中 ...

  2. Android 第3方控件一览表

    1 UnSlideListView 解决在ScrollView的无法正常显示的问题 例子在“真好项目”中“NGDetailActivity”.“HKcfqjActivity”.

  3. ListView遍历每个Item出现NullPointerException的异常(转)

    在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerExc ...

  4. 智能车学习(十八)——电机学习

    一.C车电机选择 1.摘要:      因为C车模在四轮车的优势是有两个电机,可以进行主动差速,劣势是电机太弱了....所以如何选择电机,就是个钱的问题了,电机多一点,就比较好选,但是C车电机跑多了就 ...

  5. Jmeter之录制脚本(二)

    上一节已经已经介绍过Jmeter的安装,对于web测试的话,经常会用到一些脚本去执行某些功能,也就是所谓的半自动化测试, 对于不懂代码的童鞋来说,脚本是一个很头疼的概念,badboy的录制是一个对于刚 ...

  6. Fragemnt和TextView的交互(TextView在LinearLayout中)

    import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android ...

  7. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

  8. 8659 Mine Sweeping

    时间限制:500MS  内存限制:65535K提交次数:37 通过次数:15 题型: 编程题   语言: G++;GCC Description The opening ceremony of the ...

  9. Android系统中的6种模式

    Android系统中的6种模式 1:一般启动模式(normal mode):    功能是正常启动手机,方法为关机状态下按电源键启动. 2:安全模式(safe mode):    此模式和正常启动一样 ...

  10. Mac OS Storm+Kafka+Zookeeper配置

    先补充一个前两天mac配置的文档. 首先确定由jdk scala环境 JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/Cu ...