无重复字符的最长子串

 Longest Substring Without Repeating Characters

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.

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

示例:

给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3。

给定 "bbbbb" ,最长的子串就是 "b" ,长度是1。

给定 "pwwkew" ,最长子串是 "wke" ,长度是3。请注意答案必须是一个子串,"pwke" 是 子序列  而不是子串。

 

思路:1.子串2.没有重复字符的子串

   使用 indexOf(char ch,int fromIndex) 函数 来判断前面的子串中是否有包含字符,然后不断循环设置

代码如下:

class Solution2{
    public int lengthOfLongestSubstring(String s) {
        if(s.equals(""))//字符串为空的时间
            return 0;
        int length = 1;
        int p_length = 1;
        char[] chars = s.toCharArray();//字符串变为 字符数组 取出字符
        for(int i=1;i<chars.length;i++){
//            System.out.print(chars[i]+"\t");
            for(int j=i;j<chars.length;j++){
                int index = s.indexOf(chars[j],i-1);//判断 字符的位置 如果没有该字符 则返回 -1
//                System.out.println(index);
                if(index==-1&&index>=j){// 如果为 -1那么 不存在该数值  如果大于等于j 说明此字符前面的字串 不存在该字符
                    p_length++;
                }else{
                    break;
                }
            }
            if(length<p_length) {
                length = p_length;
            }
            p_length = 1;
        }
        return length;
    }
}
public class LongestSubstringWithoutRepeatingCharacters {
    public static void main(String[] args){
        Solution2 s2 = new Solution2();
        int length = s2.lengthOfLongestSubstring("bbbbbbbb");
        System.out.print(length);
    }
}
//执行用时:112 ms 算是里面时间非常长的了 主要是使用了两次循环
//使用 HashMMap的话 ,由于其中含有containsKey函数,所以,时间复杂度会降低,空间复杂度增加.

 LeetCode 给出了两种方法,暴力法 和 滑动窗口方法

//滑动窗口方法  使用HashSet是一个没有重复元素的集合
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));//右边增加
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));//滑动窗口 顾名思义,左边移除
            }
        }
        return ans;
    }
}
//优化的滑动窗口方法
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>(); // current index of character
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }
}

  

 

posted @ 2018-07-02 16:35  Echonana  阅读(925)  评论(0编辑  收藏  举报