题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/?tab=Description
Description
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
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.
My Answer
int lengthOfLongestSubstring(char* s) {
char c;
int max_length = 0;
int count = 0;
int length = strlen(s);
int i, j, start, end;
if(length == 0 || length == 1)
{
return length;
}
else
{
start = end = 0;
for( i = 1; i < length; i++ ) // 引入end后一位
{
c = s[ i ];
for( j = start; j <= end ; j++ )
{
if( c == s[j] )
{
start = j+1;
break;
}
}
end = i;
count = end - start +1;
if( count > max_length )
{
max_length = count;
}
}
return max_length;
}
}
Submission Details
983 / 983 test cases passed.
| Status: Accepted |
Runtime:
19 ms
|
Submitted:
3 minutes ago
|