本文共 884 字,大约阅读时间需要 2 分钟。
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
class Solution { public: int lengthOfLongestSubstring(string s) { int i = 1; int pos = 0; string sub; sub = s.substr(0); cout<<= 1) { while (s[i] != '\0') { sub = s.substr(pos, length); // cout << sub << endl; if (sub.find(s[i]) == -1) { //在当前子串中没有 length++; max = (max > length) ? max : length; // cout << s[i]<<" "< << " " << int(sub.find(s[i])) <<" pos="< < = i - pos) ? 1 : 0; if (flag == 1) { max = (max > length) ? max : length; } else { max = (max > p + 1) ? max : p + 1; } length = i - p - pos; pos = pos + p + 1; // printf("@pos=%d p=%d i=%d length=%d max=%d\n", pos, p, i, length, max); } i++; } max = (max > length) ? max : length; return max; } else return 0; }};
转载地址:http://ceugn.baihongyu.com/