leetcode48_最长不重复的子串

public int lengthOfLongestSubstring(String s) {
        // 开一个hashmap来维护情况,进行一个N*N的搜索
        Map<Character, Boolean> map;
        int len = s.length();
        if(len == 0) return 0;
        int[] board = new int[len];
        int ans = 1;
        for(int i = 0; i < s.length(); i++) {
            Character chi = s.charAt(i);
            map = new HashMap<>();
            map.put(chi, true);
            board[i] = 1;
            for(int j = i+1; j < len; j++) {
                Character chj = s.charAt(j);
                if(!map.containsKey(chj) || !map.get(chj)) {
                    map.put(chj, true);
                    board[i] = board[i] + 1;
                    ans = Math.max(ans, board[i]);
                }
                else break;
            }
            map.put(chi, false);
        }
        return ans;
    }
posted @ 2022-02-07 22:09  明卿册  阅读(12)  评论(0编辑  收藏  举报