zoukankan      html  css  js  c++  java
  • 【LeetCode】3. 无重复字符的最长子串

    题目

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

    示例 1:

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

    示例 2:

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

    示例 3:

    输入: "pwwkew"
    输出: 3
    解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
         请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
    

    本题同【剑指Offer】面试题48. 最长不含重复字符的子字符串

    思路一:暴力遍历

    以每个字符开始寻找最长子串。

    代码

    时间复杂度:O(n^2)

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            if (s.empty()) {
                return 0;
            }
            int size = s.size();
            unordered_set<int> uset;
            int res = 1;
            for (int i = 0; i < size; ++i) {
                uset.clear();
                int c = 0;
                for (int j = i; j < size; ++j) {
                    if (uset.count(s[j]) > 0) {                    
                        break;
                    } else {
                        uset.insert(s[j]);
                        ++c;
                    }
                }
                res = max(res, c);
            }
            return res;
        }
    };
    

    思路二:滑动窗口

    将哈希表作为滑动窗口
    如果 s[j] 在子串 [i, j - 1] 中,因为子串 [i, j - 1] 已经检查过没有重复字符,所以,在将i向前移动后,[i + 1, j - 1] 也不存在重复字符, 所以 j 不需要再次从 i + 1 位置向后检查,还是判断是否在子串 [i + 1, j - 1] 中。

    代码

    时间复杂度:O(n),最坏情况下每个位置字符没i和j都访问一次。

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int size = s.size();
            unordered_set<int> uset;
            int res = 0, i = 0, j = 0;
            while (i < size && j < size) {
                if (uset.count(s[j]) > 0) {                
                    uset.erase(s[i]);
                    ++i;
                } else {                
                    uset.insert(s[j]);
                    ++j;
                    res = max(res, j - i);
                }
            }
            return res;
        }
    };
    

    优化

    判断每个字符结尾的无重复最长子串,通过map建立字符到位置索引的映射。
    时间复杂度:O(n),

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int size = s.size();
            unordered_map<char, int> umap;
            int res = 0;
            for (int i = 0, j = 0; j < size; ++j) {
                //或 if (umap.count(s[j]) > 0) 
                if (umap.find(s[j]) != umap.end()) {
                    i = max(i, umap[s[j]] + 1);
                } 
                res =  max(res, j - i + 1);
                umap[s[j]] = j;            
            }
            return res;
        }
    };
    

    思路三:优化滑动窗口

    当数据集比较小,可以直接用数组模拟哈希表。

    代码

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int size = s.size();
            vector<int> hash(128, -1);
            int res = 0;
            for (int i = 0, j = 0; j < size; ++j) {            
                i = max(i, hash[s[j]] + 1);
                res =  max(res, j - i + 1);
                hash[s[j]] = j;            
            }
            return res;
        }
    };
    
  • 相关阅读:
    38、面向对象设计模式之策略模式(Strategy)
    37、面向对象设计模式之代理模式(Proxy)
    36、面向对象设计模式之观察者模式(Observer)
    35、面向对象设计模式之抽象工厂(Abstract Factory)设计模式
    34、面向对象设计模式之工厂模式——简单工厂模式与工厂方法模式比较
    Chisel插件
    Git 常用命令
    oh_my_zsh
    一般xcode报错
    sqlite3 语法
  • 原文地址:https://www.cnblogs.com/galaxy-hao/p/12569364.html
Copyright © 2011-2022 走看看