标签:
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 maxlen = 0, left = 0; 5 int sz = s.length(); 6 int prev[256]; 7 memset(prev, -1, 256*sizeof(int)); 8 9 for (int i = 0; i < sz; i++) { 10 if (prev[s[i]] >= left) { 11 left = prev[s[i]] + 1; 12 } 13 prev[s[i]] = i; 14 maxlen = max(maxlen, i - left + 1); 15 } 16 return maxlen; 17 } 18 };
Longest Substring Without Repeating Characters
标签:
原文地址:http://www.cnblogs.com/hexhxy/p/4790115.html