美文网首页
Java 无重复字符的最长子串

Java 无重复字符的最长子串

作者: 向祥祥 | 来源:发表于2020-04-14 10:45 被阅读0次

问题

给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。

示例

输入:“xyzxyzwabxx”
输出:6

代码

public class LongestSubstringWithoutRepeatingCharacters {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("xyzxyzwabxx"));
    }
    public static int lengthOfLongestSubstring(String s) {
        int max=0;
        Queue<String> queue = new LinkedList<>();
        for (int i=0;i<s.length();i++){
            while (queue.contains(s.substring(i,i+1))){
                queue.remove();
            }
            queue.add(s.substring(i,i+1));
            max= Math.max(queue.size(), max);
        }
        return max;
    }
}

相关文章

网友评论

      本文标题:Java 无重复字符的最长子串

      本文链接:https://www.haomeiwen.com/subject/hvkzmhtx.html