Longest Substring Without Repeating Characters

来源:互联网 发布:越前南次郎的实力数据 编辑:程序博客网 时间:2024/06/09 16:53

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.

Subscribe to see which companies asked this question.

链接:点击打开链接

用HashMap

public int lengthOfLongestSubstring(String s) {        if (s.length()==0) return 0;        HashMap<Character, Integer> map = new HashMap<Character, Integer>();        int max=0;        for (int i=0, j=0; i<s.length(); ++i){            if (map.containsKey(s.charAt(i))){                j = Math.max(j,map.get(s.charAt(i))+1);            }            map.put(s.charAt(i),i);            max = Math.max(max,i-j+1);        }        return max;    }


用Set

核心算法和上面的很类似,把出现过的字符都放入set中,遇到set中没有的字符就加入set中并更新结果res,如果遇到重复的,则从左边开始删字符,直到删到重复的字符停止

public int lengthOfLongestSubstring(String s) {       Set set=new HashSet();         int j=0,i=0,max=0;         while(i<s.length()){             if(!set.contains(s.charAt(i))){                 set.add(s.charAt(i));                 i++;                // System.out.println(set);                 max=Math.max(max,set.size());                //System.out.println(max);             }else{                 set.remove(s.charAt(j));                 j++;                             }         }         return max;    }

还有一种很巧妙地方法  复杂度只有O(n)

建立一个256位大小的整型数组来代替哈希表,这样做的原因是ASCII表共能表示256个字符,所以可以记录所有字符,然后我们需要定义两个变量res和left,其中res用来记录最长无重复子串的长度,left指向该无重复子串左边的起始位置,然后我们遍历整个字符串,对于每一个遍历到的字符,如果哈希表中该字符串对应的值为0,说明没有遇到过该字符,则此时计算最长无重复子串,i - left +1,其中i是最长无重复子串最右边的位置,left是最左边的位置,还有一种情况也需要计算最长无重复子串,就是当哈希表中的值小于left,这是由于此时出现过重复的字符,left的位置更新了,如果又遇到了新的字符,就要重新计算最长无重复子串。最后每次都要在哈希表中将当前字符对应的值赋值为i+1

 public int lengthOfLongestSubstring(String s) {        int[] m = new int[256];        Arrays.fill(m, -1);        int res = 0, left = -1;        for (int i = 0; i < s.length(); ++i) {            left = Math.max(left, m[s.charAt(i)]);            m[s.charAt(i)] = i;            res = Math.max(res, i - left);        }        return res;    }


0 0
原创粉丝点击