当前位置:网站首页>Daily question 2 12

Daily question 2 12

2022-07-05 02:50:00 mujiaoniao

I'm not alone

Catalog

Lee_3: Longest character :


Lee_3: Longest character :

The idea of sliding windows , Ensure that the characters in the window meet the requirements of the topic , Reject the non-conforming questions directly

Power button

 public int lengthOfLongestSubstring(String s) {
        // The longest substring without repetition 
        int len = s.length();
        if (len == 0 ) return 0;
        Map<Character,Integer> map = new HashMap<>();
        int max  = 0;
        int left  = 0;
        for (int i = 0; i < len; i++) {
            if (map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
             // Store the value of the subscript .
            max = Math.max(max,i-left+1);// Through constant progress on left Subscript control   Eliminate the existing characters ;
 
        }
        return max;
    }

原网站

版权声明
本文为[mujiaoniao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140852083790.html