Tengmu 2022-02-13 08:51:33 阅读数:828
The sliding window —— Longest substring without repeating characters
public int lengthOfLongestSubstring(String s) {
char[] c = s.toCharArray();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int max = 0;
for(int start=0,end=0;end<c.length;end++){
if(map.containsKey(c[end])){
start = Math.max(start, map.get(c[end]));
}
max = Math.max(max, end-start+1);
map.put(c[end], end+1);
}
return max;
}
map.put(c[end], end+1)
: If it happens againc[end]
, From the last timec[end]
Location of occurrenceend
The next position of , Prevent duplicate characters in the window .
copyright:author[Tengmu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130851310524.html