https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
最长不重复字符串
1.关键思想是position = new int[26];
2.for循环初始化为 -1
3.然后是for循环计数,关键思想是记住position[str-‘a’],只遍历一遍,时间复杂度是O(n);
4.最后如果直接遍历到最后,记得把curLength赋给maxLength
以下是伪码
//第一步
int[] position = new int[26];
//for循环初始化为负一
position = -1;
//然后是for循环计数,只遍历一遍,时间复杂度是O(n);
for(int i = 0; i<str.length; i++){
if(position[str[i]-'a']<0 && i-position[str[i]-'a']>curLength)
curLength++;
else
if(curLength>maxLength)
maxLength = curLength;
position[str[i]-'a'] = i;
}
//如果直接遍历到最后,记得把curLength赋给maxLength
if(curLength>maxLength)
maxLength = curLength;
//return
return maxLength;