博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
查找出一个字符串不重复字符的最大长度
阅读量:4181 次
发布时间:2019-05-26

本文共 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; } HashMap
map = 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/

你可能感兴趣的文章
火爆国外的Python教程,终于迎来了汉化版!
查看>>
VS Code 真的会一统江湖吗?
查看>>
百度网盘这个版本绝了,简洁无广告,速度还快
查看>>
Linux之父警告全球程序员:我刚发布的5.12内核有bug,你们千万别用
查看>>
IDEA公司真牛逼,发行最适合程序员编程字体~
查看>>
闲鱼 12 块买的软件资源!禁止贩卖赚钱
查看>>
刷LeetCode算法题的常见模式套路
查看>>
GitHub 标星8.6K:将任何设备转换为电脑的辅助屏幕
查看>>
955 互联网公司白名单来了!这些公司月薪20k,没有996!福利榜国内大厂只有这家!...
查看>>
这种古法制作CPU,看第一眼就被震撼到了
查看>>
注意!某知名国产软件被曝携带木马病毒
查看>>
为什么我建议你一定要学Python?
查看>>
哈哈哈哈哈哈…发现微信一个bug!有点好玩~
查看>>
B 站,真香 ! ! !
查看>>
我是如何放弃 JSP,转向 REST 编程的
查看>>
阿里二面:什么是mmap?
查看>>
收到字节 Offer,月薪 45k,爽!
查看>>
计算机科学界至今未解决的四大难题
查看>>
程序员离职后躲老家山洞 2 年,敲出 45 万行代码...
查看>>
7 面 Google,还是失败了....
查看>>