面试题12:字符串无重复子串的最大长度
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.
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 int* m = new int[256]; 5 for (int i = 0; i < 256; i++) { 6 m[i] = -1; 7 } 8 int res = 0; 9 int start = -1; 10 int n = s.length(); 11 for (int i = 0; i < n; i++) { 12 start = max(start, m[s[i]]); 13 res = max(res, i - start); 14 m[s[i]] = i; 15 } 16 return res; 17 } 18 19 };