LeetCode - 3.Longest Substring Without Repeating Characters(388ms)

Posted LZ_Jaja

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode - 3.Longest Substring Without Repeating Characters(388ms)相关的知识,希望对你有一定的参考价值。

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int l = s.length();
 5         map<char, int> m;
 6         int innerL = 0;
 7         int maxL = 0;
 8         for(int i=0; i<l; i++) {
 9             map<char, int>::iterator it = m.find(s[i]);
10             if(it != m.end()) {
11                 if(maxL < innerL) {
12                     maxL = innerL;
13                 }
14                 i = i - innerL + it->second - 1;
15                 innerL = 0;
16                 m.erase(m.begin(), m.end());
17             }
18             else {
19                 innerL += 1;
20                 m[s[i]] = innerL;
21             }
22         }
23         return (innerL < maxL) ? maxL : innerL;
24     }
25 };

 

以上是关于LeetCode - 3.Longest Substring Without Repeating Characters(388ms)的主要内容,如果未能解决你的问题,请参考以下文章

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]

leetcode 3. Longest Substring Without Repeating Characters (Python版)

LeetCode - 3 - Longest Substring Without Repeating Characters

[LeetCode]3. Longest Substring Without Repeating Characters

[leetcode]3-Longest Substring Without Repeating Characters