首页 > 其他分享 >无重复最长字符串

无重复最长字符串

时间:2023-02-17 19:56:47浏览次数:35  
标签:map end start 重复 max int 字符串 最长

class Solution {
    public int lengthOfLongestSubstring(String s) {   
        int len = s.length();   // 输入的字符串长度
        int max = 0;   //  不重复的字符串数量
        int start = 0;          // 不重复字符串的起始位置
        Map<Character, Integer> map = new HashMap<>();  // 保存字符
        for(int end = 0; end < len ; end ++){
           if( map.containsKey(s.charAt(end))){
               start = Math.max(start, map.get(s.charAt(end)) + 1 );
           }
           map.put(s.charAt(end) , end);
           max = Math.max(max , end - start + 1);
        }
        return max;
    }
}

题目:https://leetcode.cn/problems/longest-substring-without-repeating-characters/submissions/

标签:map,end,start,重复,max,int,字符串,最长
From: https://www.cnblogs.com/luyj00436/p/17131375.html

相关文章