-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.c
More file actions
29 lines (21 loc) · 837 Bytes
/
Copy path3.c
File metadata and controls
29 lines (21 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
int lengthOfLongestSubstring(char *s) {
int len = strlen(s);
if (len == 0) {
return 0;
}
int maxLength = 0;
int start = 0; // 记录当前子串的起始位置
int charIndex[128]; // 用于记录字符在字符串中的最后出现位置
memset(charIndex, -1, sizeof(charIndex)); // 初始化为-1,表示字符尚未出现过
for (int end = 0; end < len; end++) {
// 如果当前字符已经在子串中出现过,更新子串的起始位置
start = (charIndex[s[end]] >= start) ? charIndex[s[end]] + 1 : start;
// 更新字符的最后出现位置
charIndex[s[end]] = end;
// 更新最大长度
maxLength = (end - start + 1 > maxLength) ? end - start + 1 : maxLength;
}
return maxLength;
}