Longest Substring Without Repeating Characters

来源:互联网 发布:windows 数据恢复 编辑:程序博客网 时间:2024/06/03 18:20

题意:
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) {        const int SIZE=256;        int record[SIZE];        memset(record,-1,sizeof(record));// 当顺序遍历到字符 s[i],s[i] 最后出现的位置         int final_ans=0;        int new_start=0; // 这一轮不重复字符串的起点        for(int i=0;i<s.size();++i){            if(record[(int)s[i]]>=new_start)                new_start=record[(int) s[i]]+1;//新的起点            if(i-new_start+1>final_ans) // 更新 final_ans                final_ans=i-new_start+1;            record[(int)s[i]]=i; // 更新 s[i] 最后一次出现的地方        }        return final_ans;    }};
0 0