给定一个字符串,求出其中最大的子串长度,子串中要求所有字符只出现一次。
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
思路:
运用2个下标,一个遍历字符串,一个记录子串的起始位置,每次将遍历的字符与子串中的字符挨个比较,若当前字符与子串中有相等的,则更新子串的起始位置,每一次循环后,都比较当前的子串长度与之前的最大长度,若更大,则更新,否则不更新。
int lengthOfLongstSubstring(string s) { int res = 0, l = 0; for (int i = 0; i < (int)s.size(); i++) { for (int j = l; j < i; j++) { if (s[i] == s[j]) { l = j + 1; break; } } res = i - l + 1 > res ? i - l + 1 : res; } return res; }
3. Longest Substring Without Repeating Characters
原文:https://www.cnblogs.com/luo-c/p/12896144.html