lintcode:Longest Substring Without Repeating Characters
2015-05-17 17:55
288 查看
Given a string, find the length of the longest substring without repeating characters.
Example
For example, the longest substring without repeating letters for
which the length is
For
longest substring is
Challenge
O(n) time
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
class Solution { public: /** * @param s: a string * @return: an integer */ int lengthOfLongestSubstring(string s) { // write your code here vector<int> dp(s.size()+1); map<int, int> posMap; dp[0] = 0; int retMax = 0; for (int i=0; i<s.size(); i++) { if (posMap.count(s.at(i)) == 0) { dp[i+1] = dp[i]+1; } else { dp[i+1] = min(i-posMap[s.at(i)], dp[i]+1); } posMap[s.at(i)] = i; retMax = max(retMax, dp[i+1]); } return retMax; } };
相关文章推荐
- lintcode-medium-Longest Substring Without Repeating Characters
- [LintCode] Longest Substring Without Repeating Characters
- [Lintcode] Longest Substring Without Repeating Characters 最长无重复字符的子串
- Leetcode [Longest Substring Without Repeating Characters]
- Leetcode代码学习周记——Longest Substring Without Repeating Characters
- Longest Substring without repeating Characters(12.5)
- Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- leetcode——3 Longest Substring Without Repeating Characters
- [Leetcode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters 没有重复字符的最长子字符串
- LeetCode 3 Longest Substring Without Repeating Characters
- LeetCode OJ算法题(三):Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters**
- Longest Substring Without Repeating Characters & Rotate List
- leetcode_java_Longest Substring Without Repeating Characters
- Leetcode日记(3)---Longest Substring Without Repeating Characters
- 2015-07-13 leetcode:Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- [LeetCode]003-Longest Substring Without Repeating Characters