思路:
用HashMap动态存储字符串的每个不重复的子字符串;
当有重复字符出现时,动态更新子字符串的最大值和更新HashMap
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int removeStart = 0,
max = 0,
i = 0;
for(;i < s.length();i++) {
char ch = s.charAt(i);
if(!map.containsKey(ch)) {
map.put(ch,i);
} else{
max = Math.max(max,map.size());
while(map.containsKey(ch)) {
map.remove(s.charAt(removeStart));
removeStart++;
}
map.put(ch,i);
}
}
// 当字符串无重复时,max还是0,则需要更新
max = Math.max(max, map.size());
return max;
}
}
本文介绍了一种使用HashMap实现的高效算法,用于求解给定字符串中最长无重复字符子串的长度。通过动态更新子字符串并利用HashMap存储已出现的字符位置,该算法能在O(n)的时间复杂度内解决问题。
697

被折叠的 条评论
为什么被折叠?



