在两个相同字符中查找最长的间距
class Solution {
public int lengthOfLongestSubstring(String s){
//记录每个字符开始的索引
int[] last = new int[128];
for(int i = 0; i < 128; i++) {
last[i] = -1;
}
int n = s.length();
int res = 0;
int start = 0;
for(int i = 0; i<n; i++){
int index = s.charAt(i);
start = Math.max(start, last[index]+1);
res = Math.max(res, i-start+1);//两个相同字符间隔的字符个数
last[index] = i ;//记录上一次出现的索引位置
}
return res;
}
}