String data structure and algorithm

Tengmu 2022-02-13 08:51:33 阅读数:828

string data structure algorithm

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++){


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 again c[end], From the last time c[end] Location of occurrence end The next position of , Prevent duplicate characters in the window .

copyright:author[Tengmu],Please bring the original link to reprint, thank you.