码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2015-04-14 23:12:41      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
};

 

Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/aceg/p/4426224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!