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.
int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int longest = 0; int slen = s.length(); if(slen < 0) return 0; if(slen == 1) return 1; int pre = 1; for(int i = 1; i < slen; ++i) { char tmp = s[i]; int j = i-1; int count = 1; while((j>=0) && (tmp != s[j])) {j--;count++;} if(count >= pre+1) count = pre+1; if(count > longest) longest = count; pre = count; } return longest; }
作者:doc_sgl 发表于2013-9-28 1:43:38 原文链接
阅读:158 评论:0 查看评论