首页 > 其他 > 详细

求字符串最大不重复串

时间:2020-04-19 10:44:24      阅读:81      评论:0      收藏:0      [点我收藏+]
import java.util.*;
public class Client {
    public static void main(String[] args) {
        String str = "abcdbe";
        int i = maxSubLen(str);
        System.out.println(i);
    }

    /**
     * 求不重复字符的长度
     */
    static int maxSubLen(String str) {
        int maxLen = 0;
        int left = 0;
        int right = 0;
        char[] chars = str.toCharArray();
        int n = str.length();
        Object mapVal = new Object();
        Map<Character, Object> map = new HashMap<>();

        while (right < n) {
            Object val = map.get(chars[right]);
            if (val == null) {// 没有重复字符
                map.put(chars[right], mapVal);
                right++;
                maxLen = Math.max(right - left, maxLen);
            } else {// 有重复字符
                while (chars[left] != chars[right]) {
                    left++;
                }
                left++;
                right++;
            }
        }
        return maxLen;
    }

}

 

求字符串最大不重复串

原文:https://www.cnblogs.com/dongma/p/12730170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!