1. 程式人生 > >算法 - 最長無重復子串

算法 - 最長無重復子串

bsp title ref new 重復 script while hashmap scrip

原題:

https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

參考:

[LeetCode] Longest Substring Without Repeating Characters 最長無重復子串

解法:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        
        
int res = 0, i = 0,left=0; while(i<s.length()) { char ch = s.charAt(i); if(map.containsKey(ch) && map.get(ch)>=left) left = map.get(ch)+1; else res = Math.max(res,i-left+1); map.put(ch,i++); } return res; } }

算法 - 最長無重復子串