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.
存在子串
若
// 找到c在s[from, end]中的位置
int find(char *s, int from, int end, char c) {
int i;
for (i = from; i <= end; ++i) {
if (s[i] == c) {
return i;
}
}
return -1;
}
int lengthOfLongestSubstring(char* s) {
int length_s = strlen(s);
if (length_s < 2) {
return length_s;
}
int longest = 1;
int tmp_count = 1;
int i, j;
int k;
for (i = 0, j = 1; j < length_s; ++j) {
k = find(s, i, j - 1, s[j]);
if (k == -1) {
tmp_count++;
continue;
} else {
if (tmp_count > longest) {
longest = tmp_count;
}
i = k + 1;
tmp_count = j - i + 1;
}
}
if (tmp_count > longest) {
longest = tmp_count;
}
return longest;
}Characters
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。