题目:

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     vector<int> dict(256, -1);
 5         int maxLen = 0, start = -1;
 6         for (int i = 0; i != s.length(); i++) {
 7             if (dict[s[i]] > start)
 8                 start = dict[s[i]];
 9             dict[s[i]] = i;
10             maxLen = max(maxLen, i - start);
11         }
12         return maxLen;
13     }
14 };

 

相关文章:

猜你喜欢
  • 2021-08-23
  • 2021-07-09
  • 2021-08-13
  • 2021-12-09
相关资源
相似解决方案