0
点赞
收藏
分享

微信扫一扫

【每日一题】【滑动窗口】2021年11月12日-3. 无重复字符的最长子串※

给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。

【每日一题】【滑动窗口】2021年11月12日-3. 无重复字符的最长子串※_javascript

注意:containsKey

class Solution {
public int lengthOfLongestSubstring(String s) {
Map map = new HashMap<>();
int left = 0;
int max = 0;
for(int i = 0; i < s.length(); i ++) {
if(map.containsKey(s.charAt(i))) { //包含当前字符,就找left和包含当前字符的最大值作为left
left = Math.max(left, map.get(s.charAt(i)) + 1);
}
map.put(s.charAt(i), i); //更新当前字符的位置为i
max = Math.max(max, i - left + 1); //对比当前位置和有重复字符开始位置left之间的距离最大值【每次都会比较】
}
return max;
}
}

 


作者:哥们要飞​


举报

相关推荐

0 条评论