public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() < 1) {
return 0;
}
char[] chars = s.toCharArray();
int prevMax = 0;
Map<Character/*char of chars*/,Integer/*index of char*/> map = new HashMap<>();
for (int i = 0; i < chars.length; i++) {
if (map.containsKey(chars[i])) {
prevMax = Math.max(map.size(), prevMax);
i = map.get(chars[i]);
map.clear();
} else {
map.put(chars[i], i);
}
}
return Math.max(map.size(), prevMax);
}
}
Longest Substring Without Repeating Characters
最新推荐文章于 2024-01-06 13:50:14 发布