LeetCode -- 3. Longest Substring Without Repeating Characters--java

陆陆续续在LeetCode上刷了一些题,一直没有记录过,准备集中整理记录一下

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() < 2) {
            return s.length();
        }
        int max = 0;
        List list = new ArrayList<>();
        for (int i = 0; i < s.length() - 1; i++) {
            list.clear();
            for (int j = i ; j < s.length(); j++) {
                if (!list.contains(s.charAt(j))) {
                    list.add(s.charAt(j));
                    max = Math.max(max, list.size());
                } else {
                    break;
                }
            }
        }
        return max;
}
}

你可能感兴趣的:(LeetCode -- 3. Longest Substring Without Repeating Characters--java)