1.利用滑动窗口(Set)
2.直接计算两重复元素间的间隔
Java (Assuming ASCII 128)
The previous implements all have no assumption on the charset of the string s
.
If we know that the charset is rather small, we can replace the Map
with an integer array as direct access table.
Commonly used tables are:
int[26]
for Letters 'a' - 'z' or 'A' - 'Z'int[128]
for ASCIIint[256]
for Extended ASCII
public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[128]; // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
}