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.
字符串处理
class Solution { public: int lengthOfLongestSubstring(string s) { vector<int> bar(200, -1); int N = s.size(); int result = 0; int l = 0, r = 0; while (r < N) { if (bar[s[r]]>=l) { result = max(result, r - l); l = bar[s[r]] + 1; } bar[s[r] ] = r; r++; } result = max(result, r - l); vector<int>().swap(bar); return result; } };
时间: 2024-10-05 04:19:09