Longest Substring Without Repeating Characters

来源:互联网 发布:办公软件应用2003 编辑:程序博客网 时间:2024/05/16 17:45

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.

===================================================================================================================================

找到最长的不重复子字符串,思路就是找个数组保存每个字符出现的次数,大于1时就证明重复了,剪掉之后继续找

int lengthOfLongestSubstring(char* s) {    static bool flag[256];memset(flag, false, sizeof(flag));int len = 0, max = 0;for (int i = 0; i <strlen(s); i++){if (!flag[s[i]]){flag[s[i]] = true;len++;}else{int counter = 0;for (int j = i - len;s[j]!=s[i];j++){flag[s[j]]=false;counter++;}len -= counter;}if (max < len){max = len;}}return max;}


0 0