• 滑动窗口算法-2


    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度?
    输入: "abcabcbb"
    输出: 3
    解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

    public static void main(String[] args) {
            System.out.println(t());
        }
    
    
        public static int t() {
            String st = "abcabcbb";
            int startIndex = 0;
            int endIndex = 0;
            int strMaxLen = 0;
            while (endIndex <= st.length() && startIndex <= endIndex) {
                boolean flag = repetitionSubStr(st, startIndex, endIndex);
                System.out.println("===" + flag);
                if (flag) {
                    startIndex++;
                }
                if (!flag) {
                    strMaxLen = strMaxLen > st.substring(startIndex, endIndex).length() ? strMaxLen : st.substring(startIndex, endIndex).length();
                    endIndex++;
                }
            }
            return strMaxLen;
        }
    
        public static boolean repetitionSubStr(String orgStr, int startIndex, int endIndex) {
            String orgStrTemp = orgStr.substring(startIndex, endIndex);
            System.out.print("orgStrTemp=" + orgStrTemp + "====endIndex=" + endIndex);
            char[] orgStrCharArray = orgStrTemp.toCharArray();
            char[] sub = new char[orgStr.length()];
            for (char s : orgStrCharArray) {
                for (int i = 0; i < sub.length; i++) {
                    if (s == sub[i]) {
                        return true;
                    }
                    if ('' == sub[i]) {
                        sub[i] = s;
                        break;
                    }
                }
            }
            return false;
        }

    算法参考:https://www.zhihu.com/question/314669016

  • 相关阅读:
    python爬取图片
    IDEA创建SpringBoot项目报错,不能连接https://start.spring.io/
    ES模块化的理解
    Web标准(网站标准)理解
    Mongodb安装
    Linux Ntp时间同步服务
    SentinelResource 注解
    Sentinel的流量控制
    Sentinel简介
    nacos安装
  • 原文地址:https://www.cnblogs.com/use-D/p/13278290.html
Copyright © 2020-2023  润新知