Skip to content

Commit 16acc4b

Browse files
add problem 3 and test function
1 parent c23dbee commit 16acc4b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Given a string, find the length of the longest substring without repeating characters.
3+
*
4+
* Examples:
5+
* Given "abcabcbb", the answer is "abc", which the length is 3.
6+
* Given "bbbbb", the answer is "b", with the length of 1.
7+
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
8+
* Created by supercoderhawk on 2017/7/23.
9+
*/
10+
#include
11+
#include "../main.h"
12+
13+
14+
int lengthOfLongestSubstring(char* s) {
15+
int start =0, end = 0,digit = 0, maxLength = 0;
16+
char* p = s;
17+
int letters[128];
18+
for(int i = 0; i < 128; i++)
19+
letters[i] = 0;
20+
21+
while(*p != '\0')
22+
{
23+
digit = (int)*p;
24+
while(letters[digit] != 0)
25+
{
26+
letters[(int)s[start]] = 0;
27+
start += 1;
28+
}
29+
letters[digit] = 1;
30+
end += 1;
31+
if (end - start > maxLength)
32+
maxLength = end-start;
33+
p = p + 1;
34+
}
35+
return maxLength;
36+
}
37+
38+
void testLengthOfLongestSubstring()
39+
{
40+
char *p="pwwkew";
41+
printf("%d",lengthOfLongestSubstring(p));
42+
}

0 commit comments

Comments
 (0)