题目来源:
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
题目描述:
代码如下:
class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> hashMap = new HashMap<>();
int res = 0, left = 0;
for (int i = 0; i < s.length(); i++) {
if (hashMap.containsKey(s.charAt(i))) {
left = Math.max(left, hashMap.get(s.charAt(i)));
}
res = Math.max(res, i - left + 1);
hashMap.put(s.charAt(i), i + 1);
}
return res;
}
}