publicintlengthOfLongestSubstring(String s){ if (s == null || s.length() == 0){ return0; } LinkedList<Character> queue = new LinkedList<>(); queue.offer(s.charAt(0)); int index = 1; int max = 1; while (index < s.length()){ char temp = s.charAt(index); int s1 = isInQueue(queue, temp); if (s1 >= max){ max = s1; } index ++; } return max; } // 返回队列中与目标字符相同字符的距离,即遍历到当前字符时最长不重复长度 privateintisInQueue(LinkedList<Character> queue, char c){ LinkedList<Character> queueT = new LinkedList<>(); int flag = 0; while (!queue.isEmpty()){ if (queue.peekLast()!=c){ queueT.offer(queue.pollLast()); flag ++; } else{ while(!queue.isEmpty()){ queue.poll(); } } } while (!queueT.isEmpty()){ queue.offer(queueT.pollLast()); } queue.offer(c); return flag + 1; }