Longest Substring Without Repeating Characters

来源:互联网 发布:手机壁纸大全软件 编辑:程序博客网 时间:2024/06/07 04:56

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.


<span style="color:#333333;">class Solution {public:    int lengthOfLongestSubstring(string s) {        int a[256];        memset(a,0,256*4);        int i=0;        int sum=0;        int max=0;        while(i<s.length()){            if(a[(int)s[i]]==0){                </span><span style="color:#ff0000;">a[(int)s[i]]</span><span style="color:#333333;">=i+1;//存储保证字符出现的下标,从1开始,是为了避免和a[(int)s[i]]==0冲突                sum++;                if(sum>max){                    max=sum;                }                i++;            }else{                </span><span style="color:#ff6666;">i=a[(int)s[i]];</span><span style="color:#333333;">                memset(a,0,256*4);                sum=0;            }        }        if(sum>max){            max=sum;        }        return max;    }};</span>


0 0
原创粉丝点击