/**
* This is an improved version over the previous submission: https://www.jianshu.com/p/56eac9b1f73c
*/
void clear(int *a, char *s, int start, int end) { for (int i = start; i <= end; i++) { a[s[i]] = -1; } }
#define R 128//capacity of the character set
int lengthOfLongestSubstring(char * s) {
if (s == NULL || *s == '\0') return 0;
int n = strlen(s);
int *a = (int*)malloc(R * sizeof(*a));
for (int i = 0; i < R; i++) { a[i] = -1; }
int len = 0, start = 0;
for (int i = 0; i < n; i++) {
if (a[s[i]] != -1) {
int t = i - start;
if (len < t) { len = t; }
start = a[s[i]] + 1;
clear(a, s, i - t/*i - t = start*/, a[s[i]]);
}
a[s[i]] = i;
}
int t = n - start;
if (len < t) { len = t; }
return len;
}
LeetCode #3 Longest Substring Without Repeating Characters
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Given a string, find the length of the longest substring ...
- Given a string, find the length of the longest substring ...
- 题目描述: 最长连续无重复子字符串Example 1: Input: "abcabcbb"Output: 3Exp...
- 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. 问题描述 Given a str...
- Problem Specs: Solution(Implemented in C):