剑指offer48 最长不重复子串 Java

public class LongestSubStringWithoutDuplication48__ {
    public static void main(String[] args) {
        String str = "arabcacfr";
        System.out.println(subString(str));
        int i = 0;
        i = i ++;
        System.out.println(i);
    }

    private static int subString(String str) {
        if (str.length() <= 0) {
            return 0;
        }
        int[] preIndex = new int[26];
        Arrays.fill(preIndex, -1);
        int curLength = 0;
        int maxLength = 0;
        for (int i = 0; i < str.length(); i++) {
            char curChar = str.charAt(i);
            int curCharIndexInPreIndex = curChar - 'a';
            //与当前字符重复出现的字符的 上一次出现的位置
            int curCharPreIndex = preIndex[curCharIndexInPreIndex];
            //如果当前字符之前没得出现过,或者当前字符出现过 但是两次出现之间的距离大于 以前一个字符结尾的不重复子串的长度
            if (curCharPreIndex == -1 || i - curCharPreIndex > curLength) {
                curLength++;
            } else {
                //当前字符出现过并且两次出现的距离小于 以前一个字符结尾的不重复子串的长度
                maxLength = Math.max(maxLength, curLength);
                curLength = i - curCharPreIndex;
            }
            preIndex[curCharIndexInPreIndex] = i;
        }
        return Math.max(maxLength, curLength);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43065507/article/details/99332043