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) 
    {
        int Count[256];
        int i;
        int len ;
        int maxLen = 0;
        int idx = -1;

        memset( Count, -1, sizeof(Count) );
        //cout << Count[0] << " " << Count[1] <<endl;
        len = s.size();

        for( i = 0; i < len; i++ )
        {
           if( Count[s[i]] > idx )
           {
               idx =  Count[s[i]];
           }

           if( i - idx > maxLen )
           {
                maxLen = i - idx;
           }
           Count[s[i]] = i;
        }

        return maxLen;
    }
};

 

posted on 2015-04-14 21:46  听风的日子  阅读(132)  评论(0编辑  收藏  举报