Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
思路:同向双指针, i是主指针,j是辅指针;主指针写for循环,j一直移动写while循环;
当移动到有重复值的时候,update len (j-i), 注意 int count array 的index就是char;
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) {
return 0;
}
int j = 0;
int res = 0;
int[] scount = new int[255];
for(int i = 0; i < s.length(); i++) {
// move j;
while(j < s.length() && scount[s.charAt(j)] == 0) {
scount[s.charAt(j)] = 1;
j++;
}
// update res;
res = Math.max(res, j - i);
// move i;
scount[s.charAt(i)]--;
}
return res;
}
}