LintCode:M-最长无重复字符的子串

来源:互联网 发布:有意识的人工智能 编辑:程序博客网 时间:2024/05/17 11:59

LintCode链接


给定一个字符串,请找出其中无重复字符的最长子字符串。

样例

例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3

对于,"bbbbb",其无重复字符的最长子字符串为"b",长度为1


public class Solution {    /*     * @param s: a string     * @return: an integer     */    public int lengthOfLongestSubstring(String s) {        Set<Character> set = new HashSet<Character>();        int n = s.length();        int maxLen=0;        int start=0;        int i=0;        while(i<n){            char c = s.charAt(i);            if(!set.contains(c)){                set.add(c);                i++;               }else{                set.remove(s.charAt(start++));            }            maxLen = Math.max(maxLen, set.size());        }                return maxLen;    }}


原创粉丝点击