关于java:JZ073最长不含重复字符的子字符串

最长不含反复字符的子字符串

题目形容

输出一个字符串(只蕴含 a~z 的字符),求其最长不含反复字符的子字符串的长度。例如对于 arabcacfr,最长不含反复字符的子字符串为 acfr,长度为 4。

题目链接: [最长不含反复字符的子字符串]()

代码

import java.util.Arrays;

/**
 * 题目:最长不含反复字符的子字符串
 * 题目形容
 * 输出一个字符串(只蕴含 a~z 的字符),求其最长不含反复字符的子字符串的长度。例如对于 arabcacfr,最长不含反复字符的子字符串为 acfr,长度为 4。
 */
public class Jz73 {

    public int longestSubStringWithoutDuplication(String str) {
        int curLen = 0;
        int maxLen = 0;
        int[] preIndexs = new int[26];
        Arrays.fill(preIndexs, -1);
        for (int curI = 0; curI < str.length(); curI++) {
            int c = str.charAt(curI) - 'a';
            int preI = preIndexs[c];
            if (preI == -1 || curI - preI > curLen) {
                curLen++;
            } else {
                maxLen = Math.max(maxLen, curLen);
                curLen = curI - preI;
            }
            preIndexs[c] = curI;
        }
        maxLen = Math.max(maxLen, curLen);
        return maxLen;
    }

    public static void main(String[] args) {
        
    }
}

【每日寄语】 为人子,方少时;亲师友,习礼仪。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理