本文共 1041 字,大约阅读时间需要 3 分钟。
package com.xiaobu.leetcode;import com.xiaobu.base.util.StringUtil;import java.util.HashMap;/** * @author xiaobu * @version JDK1.8.0_171 * @date on 2020/3/27 14:38 * @description 查找出一个字符串不重复字符的最大长度 * https://segmentfault.com/a/1190000016216003 */public class TheMaxLengthOfNotRepeate { public static int getTheMaxLengthOfNotRepeate(String str) { if (StringUtil.isNullOrEmpty(str)) { return 0; } HashMapmap = new HashMap<>(); //出现的位置 int max = 0; //重复的位置 int pre = -1; for (int i = 0, strLen = str.length(); i < strLen; i++) { Character ch = str.charAt(i); Integer index = map.get(ch); if (index != null) { pre = Math.max(index, pre); } max = Math.max(max, i - pre); map.put(ch, i); } return max; } public static void main(String[] args) { String str = "abcdefg"; System.out.println(getTheMaxLengthOfNotRepeate(str)); }}
转载地址:http://gwgai.baihongyu.com/