美文网首页
2022-06-20 048最长不含重复字符的子字符串

2022-06-20 048最长不含重复字符的子字符串

作者: 16孙一凡通工 | 来源:发表于2022-06-20 10:14 被阅读0次

剑指 Offer 48. 最长不含重复字符的子字符串

双指针+哈希表
右指针是for循环遍历,左指针随着哈希表判断是否重复而改变,统计每次循环两个指针的窗口长度。

class Solution {
    public int lengthOfLongestSubstring(String s) {

   Map<Integer,Integer> hashmap=new HashMap<>();
// 滑动窗口
int i=-1,res=0;
   for(int j=0;j<s.length();j++){

       if(hashmap.containsKey(s.charAt(j)-'a'))
          i=Math.max(i,hashmap.get(s.charAt(j)-'a'));
      hashmap.put(s.charAt(j)-'a',j);
    //   System.out.println("j:"+j+" i:"+i);
      res=Math.max(res,j-i);
   }
   return res;
}

}

相关文章

网友评论

      本文标题:2022-06-20 048最长不含重复字符的子字符串

      本文链接:https://www.haomeiwen.com/subject/mtzdvrtx.html