-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
40 lines (35 loc) · 780 Bytes
/
main.c
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
30
31
32
33
34
35
36
37
38
39
40
/*
* maxstr.c
*
* Created on: 2019年10月24日
* Author: gyf
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lengthOfLongestSubstring(char * s) {
int table[0xFF];
memset(table, -1, sizeof(table));
int maxlen = 0;
int idx = 0;
int startidx = 0;
int findidx = 0;
int ch;
while ((ch = s[idx]) != '\0') {
findidx = table[ch];
if (findidx >= startidx) {
int len = idx - startidx;
maxlen = len > maxlen ? len : maxlen;
startidx = findidx + 1;
}
table[ch] = idx;
++idx;
}
int len = idx - startidx;
maxlen = len > maxlen ? len : maxlen;
return maxlen;
}
int main()
{
printf("%d\n", lengthOfLongestSubstring("123a3cnbj3"));
}