java 代码
class Solution {
public int longestSubstringWithoutDuplication(String s) {
int len = s.length();
if(len == 0 || len == 1) return len;
//定义一个数组,长度位26。用于存储a-z最后一次出现的位置
int[] array = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int frist = 0;
int second = 1;
int max = 1;
while(frist < len){
int fristchar = s.charAt(frist)-97;
array[fristchar] = frist;
while(second < len){
int secondchar = s.charAt(second)-97;
if(array[secondchar] != -1){
frist = frist<=array[secondchar]? array[secondchar]+ 1:frist;
}
array[secondchar] = second;
if((second-frist + 1) > max) max = second-frist + 1;
second++;
}
frist++;
}
return max;
}
}