class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map = new HashMap<Character,Integer>();
int maxLength = 0;
int now = 0;
for(int i=0; i<s.length(); i++ ){
if(map.containsKey(s.charAt(i))){
now = Math.max(now, map.get(s.charAt(i))+1);
if((i-now+1) > maxLength){
maxLength = i-now+1;
}
}else{ //map中不存在当前字符
if((i-now+1) > maxLength){ //更新最长字串的长度
maxLength = i-now+1;
}
}
map.put(s.charAt(i), i);//修改当前字符的value,记录最新位置
}
return maxLength;
}
}
给定一个字符串,找出不含有重复字符的最长子串的长度。
最新推荐文章于 2024-05-01 11:40:17 发布