Longest Substring Without Repeating Characters (implemented in C), longestrepeating
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"
IsSubsequenceAnd not a substring.
Address: https://leetcode.com/problems/longest-substring-without-repeating-characters/
Returns the length of the longest substring. The substring cannot contain repeated characters. (The substring is continuous .)
Solution: read each character of the string in sequence. If the first occurrence occurs, the length of the current substring is + 1. Otherwise, first determine whether the current length is greater than the maximum length, and then replace the maximum length. Then
Find duplicate characters in the original string.
The Code is as follows:
1 int lengthOfLongestSubstring (char * s) {2 int maxlen = 0, currlen = 0; 3 int table [128], I, j, start = 0; 4 memset (table, 0, sizeof (table); 5 for (I = 0; s [I]! = '\ 0'; ++ I) {6 if (++ table [s [I]) = 2) {7 if (currlen> maxlen) {8 maxlen = currlen; 9} 10 for (j = start; j <I; ++ j) {// The second position after the repeated characters in the start record is 11 if (s [j] = s [I]) {12 table [s [j] = 1; 13 start = j + 1; 14 break; 15} else {16 -- currlen; 17 table [s [j] = 0; 18} 19} 20} else {21 + + currlen; 22} 23} 24 if (currlen> maxlen) {25 maxlen = currlen; 26} 27 return maxlen; 28}