3. Longest Substring Without Repeating Characters
in LeetCode with 0 comment

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.



class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m[256] = { 0 }, res = 0, left = 0;
for (int i = 0; i < s.size(); ++i) {
if (m[s[i]] == 0 || m[s[i]] < left) {
res = max(res, i - left + 1);
}
else {
left = m[s[i]];
}
m[s[i]] = i + 1;//第几位
}
return res;
}
};

Responses

From now on, bravely dream and run toward that dream.