Nicksxs's Blog

What hurts more, the pain of hard work or the pain of regret?

0%

leetcode no.3

*Longest Substring Without Repeating Characters *

description

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.

分析

源码这次是参考了这个代码,
tail 表示的当前子串的起始点位置,tail从-1开始就包括的串的长度是1的边界。其实我
也是猜的(逃

1
2
3
4
5
6
7
8
9
10
11
12
int ct[256];
memset(ct, -1, sizeof(ct));
int tail = -1;
int max = 0;
for (int i = 0; i < s.size(); i++){
if (ct[s[i]] > tail)
tail = ct[s[i]];
if (i - tail > max)
max = i - tail;
ct[s[i]] = i;
}
return max;
请我喝杯咖啡