解法 动态规化
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) return 0;
char[] chars = s.toCharArray();
return lengthOfLongestSubstring(chars);
}
private int lengthOfLongestSubstring(char[] chars) {
int maxLength = 0;
int curLength = 0;
int[] position = new int[128];
Arrays.fill(position,-1);
for(int i = 0; i < chars.length; ++i) {
int preIndex = position[chars[i]];
if(preIndex < 0 || i - preIndex > curLength) {
curLength++;
}else {
if(curLength > maxLength)
maxLength = curLength;
curLength = i - preIndex;
}
position[chars[i]] = i;
}
if(curLength > maxLength)
maxLength = curLength;
return maxLength;
}
}