LeetCode刷题-计算无重复字符的最长子串的长度

876 阅读3分钟

前言说明

算法学习,日常刷题记录。

题目连接

计算无重复字符的最长子串的长度

题目内容

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

示例 1:

输入: s = "abcabcbb"

输出: 3

解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: s = "bbbbb"

输出: 1

解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: s = "pwwkew"

输出: 3

解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。

请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

示例 4:

输入: s = ""

输出: 0

提示:

0 <= s.length <= 5 * 10^4

s由英文字母、数字、符号和空格组成

分析过程

方法1:

首先想到的是用最笨的方法,用三层for循环,以每一个字符作为开头去构造子串,每次再往前遍历判断字符是否重复,如果重复了就停止,子串构造完成,对比每个子串的长度得到最长子串长度。

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // 子串的长度
        int size = 0;

        if (s != null && !"".equals(s) && s.length() > 0) {
            // 保存子串的列表
            List<Character> charList = new ArrayList<>();
            
            // 遍历n次,获得n次最长子串
            for (int n = 0; n <= s.length() - 1; ++n) {
                // 构造最长子串
                for (int i = n; i <= s.length() - 1; ++i) {
                    char c = s.charAt(i);

                    if (i > n) {
                        // 向前对比字符,判断是否重复
                        boolean isRepeat = false;

                        for (int j = i - 1; j >= n; --j) {
                            if (charList.contains(c)) {
                                isRepeat = true;
                                break;
                            }
                        }

                        if (!isRepeat) {
                            charList.add(c);
                        } else {
                            break;
                        }
                    } else {
                        charList.add(c);
                    }
                }

                // 更新最长子串长度
                if (charList.size() > size) {
                    size = charList.size();
                }
                
                // 子串列表清空元素
                charList.clear();
            }
        }

        return size;
    }
}

提交代码后,很不幸,提示超时了。

运行结果

所以这种方法不可取。

方法2:

我们来进行优化,三层for循环太多了,下面优化成两层for循环,同样以每一个字符作为开头去构造子串,但是每次直接用列表List的contains方法就能判断出是否字符重复,减少了一层for循环,最后也是对比每个子串的长度得到最长子串长度。

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // 最长子串长度
        int size = 0;

        if (s != null && !"".equals(s) && s.length() > 0) {
            // 最长子串列表
            List<Character> charList = new ArrayList<>();
            
            // 遍历i次,获得i次最长子串长度
            for (int i = 0; i <= s.length() - 1; ++i) {
                // 构造最长子串列表
                for (int j = i; j <= s.length() - 1; ++j) {
                    char c = s.charAt(j);

                    // 判断字符是否在最长子串列表中
                    if (charList.contains(c)) {
                        break;
                    } else {
                        charList.add(c);
                    }
                }

                // 更新最长子串长度
                if (charList.size() > size) {
                    size = charList.size();
                }
                
                // 子串列表清空元素
                charList.clear();
            }
        }

        return size;
    }
}

提交代码后,执行用时817ms,时间击败5.01%的用户,内存消耗38.3MB,空间击败88.51%的用户。

运行结果

可以看到不超时了,但是运行时间不是很理想,接近超时。

方法3:

我们来继续优化,两层for循环还是太多了,下面优化成一层for循环,直接遍历一次字符串的字符就得出结果,这次不用列表List来保存子串了,改为用集合Map来保存信息,遍历字符时把字符和字符的索引保存到集合Map中,一旦遇到重复字符,证明这次的子串结束了,那么这时只需改变起始索引就能重新计算出子串的长度, 每次都更新一次子串的长度,最后得到最长子串长度。

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // 最长子串长度
        int size = 0;

        if (s != null && !"".equals(s) && s.length() > 0) {
            // 子串集合
            Map<Character, Integer> map = new HashMap<>();

            // 起始索引
            int start = 0;
            
            // 遍历字符串
            for (int i = 0; i <= s.length() - 1; ++i) {
                char c = s.charAt(i);

                // 判断字符是否在子串集合中
                if (map.containsKey(c)) {
                    int index = map.get(c) + 1;
                    
                    // 更新起始索引
                    if (index > start) {
                        start = index;
                    }
                }

                // 获取当前最长子串长度
                int length = i - start + 1;

                // 更新最长子串长度 
                if (length > size) {
                    size = length;
                }

                map.put(c, i);
            }
        }

        return size;
    }
}

提交代码后,执行用时7ms,时间击败79.49%的用户,内存消耗38.7MB,空间击败37.13%的用户。

运行结果

可以看到运行时间明显降低了很多,从原来的817ms降到7ms。

总结解题

通过两次优化,从原来的超时,优化到不超时,再优化到大幅降低运行时间,这里还会有运行时间更短的方法,欢迎指出。

原文链接

原文链接:mp.weixin.qq.com/s/59JgZtywH…