当前位置:网站首页>Long substring without repeating characters for leetcode topic resolution

Long substring without repeating characters for leetcode topic resolution

2022-06-23 06:17:00 ruochen

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

    public int lengthOfLongestSubstring(String s) {
        if (s == null) {
            return 0;
        }
        if (s.length() == 0 || s.length() == 1) {
            return s.length();
        }
        char[] c = s.toCharArray();
        int barrier = 0;
        int maxLen = 1;
        for (int i = 1; i < c.length; i++) {
            for (int j = i - 1; j >= barrier; j--) {
                if (c[i] == c[j]) {
                    barrier = j + 1;
                    break;
                }
            }
            maxLen = Math.max(maxLen, i - barrier + 1);
        }
        return maxLen;
    }
原网站

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