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

10次阅读

共计 696 个字符,预计需要花费 2 分钟才能阅读完成。

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

题目形容

输出一个字符串(只蕴含 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;
            if (preI == -1 || curI - preI > curLen) {curLen++;} else {maxLen = Math.max(maxLen, curLen);
                curLen = curI - preI;
            }
            preIndexs = curI;
        }
        maxLen = Math.max(maxLen, curLen);
        return maxLen;
    }

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

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

正文完
 0