给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度?
输入: "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