3. Longest Substring Without Repeating Characters寻找不重复的最大子串

首先弄清楚Substring和Subsequence,前者是子串,要求连续,后者是子序列,可以不连续

public int lengthOfLongestSubstring(String s) {
        /*
        一开始自己想的方法:两个指针,一个作为开始,一个用来遍历,复杂度O(N)
         */
        int l = s.length();
        if (l==0)
            return 0;
        char[] str = s.toCharArray();
        int res = 1;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < l-1; i++) {
            set.add(str[i]);
            for (int j = i+1; j < l; j++) {
                if (set.contains(str[j]))
                    break;
                set.add(str[j]);
                res = Math.max(res,set.size());
            }
            set.clear();
        }
        return res;
    }
    public int lengthOfLongestSubstring2(String s) {
        /*
        O(N)做法,自己建立哈希表来记录有没有出现过,第一次接触这种做法,要记住
        思路就是用哈希表记录当前字符最近一次出现时的index
        如果没出现重复字符串就继续遍历,每次更新res长度
        如果出现了重复,那就把最左边的坐标移到前边重复数字的下一个,然后继续遍历,res一直是记录最大长度
        与这种做法相比,第一种做法每次都会遍历很多重复的子串
         */
        //所有字符都能转化为256以内的int值,这样就可以记录所有字符
        int[] index = new int[256];
        Arrays.fill(index,-1);
        //记录 最左边坐标
        int left = 0;
        //记录结果
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            //先检测有没有重复字符,如果,left更新,比较的方法是把哈希表中的记录和left比较,取大的。如果没有出现过,那么哈希表中的记录是-1,肯定是left大
            //如果出现过,有两种情况,一种是记录没有left大,说明是和left 之前的一个重复了,这种情况没有影响 ,另一种是比left大,这样就会影响长度的判断,要更新left
            left = Math.max(left,index[s.charAt(i)+1]);
            //更新哈希表
            index[s.charAt(i)] = i;
            res = Math.max(res,i-left+1);
        }
        return res;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8158977.html

时间: 2024-10-26 05:15:17

3. Longest Substring Without Repeating Characters寻找不重复的最大子串的相关文章

[LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

Leetcode Longest Substring Without Repeating Characters python

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

problem: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the long

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode3 Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

[LeetCode] 3. 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.

LeetCode Problem 3.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.