暴力滑块方法
1. 初始定义两个指针,start、end 都指向字符串开头
2. end向前走一步,就轮循检查end位置字符是否在start,end区间出现过,如果出现就将start更新为出现位置+1
3. 定义全局变量length,结果ret,ret = max(ret, length),这样找到最大的length
c++代码
class Solution { public: int lengthOfLongestSubstring(string s) { int start = 0, end = 0, length = 0, ret = 0; int size = s.size(); while(end < size){ char tmp = s[end]; for(int i=start; i<end; i++){ if (tmp == s[i]){ start = i + 1; length = end - start; break; } } end++; length++; ret = max(ret, length); } return ret; } };