LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

简介: 题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 Given a string, find the length of the longest substring without repeating characters. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

题目:

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

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

示例 1:

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

示例 2:

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

示例 3:

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

解题思路:

  • 暴力求解, 时间复杂度为 O(n^3), 因为要对所有字符遍历, 对子串遍历确认是否有重复字符, pass
  • 滑动窗口, 维护一个索引 [i,j) 的滑动窗口, 对已存在的字符 i' 直接更新滑动窗口 [i',j), 你需要保留每一个字符值及其索引, 即由字符映射索引位置

    • 哈希映射: Key 为字符值, Value 为索引位置
    • 字符映射: ASCII 码共 128 个字符, 维护一个长度为 128 的整型数组,数组索引值映射 128 个字符,存储元素值为字符位置

哈希映射:

Java:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();//转为字符数组
        if (chars.length == 0) return 0;
        HashMap<Character, Integer> map = new HashMap<>();//建立哈希映射
        int size = s.length(), count = 0, i = 0;
        for (int j = 0; j < size; j++) {//遍历字符
            if (map.containsKey(chars[j]))//如果映射中存在该字符
                i = Math.max(map.get(chars[j]), i);//更新滑动窗口的左边界 i
            count = Math.max(count, j - i);//更新 count 为最大值
            map.put(chars[j], j + 1);//更新映射中该字符映射的 Value 值为当前位置加一
        }
        return count + 1;//返回最大累加总数, 需要加 1
    }
}

Python:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if not s:
            return 0
        hash_map = dict() # 建立字典
        size = len(s)
        i, count = 0, 0
        for j, c in enumerate(s): # 枚举字符
            if c in hash_map: # 如果映射中存在该字符
                i = max(i, hash_map[c]) # 更新滑动窗口的左边界 i
            count = max(count, j-i) # 更新 count 为最大值
            hash_map[c] = j+1 # 更新映射中该字符映射的 Value 值为当前位置加一
        return count+1 # 返回最大累加总数, 需要加 1

字符映射:

Java:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        if (chars.length == 0) return 0;
        int[] index = new int[128];//建立长度为 128 位的 ASCII 码字符映射表
        int size = s.length(), count = 0, i = 0;
        for (int j = 0; j < size; j++) {//遍历字符
            i = Math.max(index[chars[j]], i);//更新滑动窗口的左边界 i
            count = Math.max(count, j - i);//更新 count 为最大值
            index[chars[j]] = j + 1;//更新映射中该字符所在元素值为当前位置加一
        }
        return count + 1;//返回最大累加总数, 需要加 1
    }
}

Python:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if not s:
            return 0
        size = len(s)
        i, count = 0, 0
        index = [0]*128 # 建立长度为 128 位的 ASCII 码字符映射表
        for j, c in enumerate(s): # 枚举字符
            i = max(i, index[ord(c)]) # 更新滑动窗口的左边界 i
            count = max(count, j-i) # 更新 count 为最大值
            index[ord(c)] = j+1 # 更新映射中该字符所在元素值为当前位置加一
        return count+1 # 返回最大累加总数, 需要加 1

因为 Python 没有字符概念, 需要 ord() 函数转为 ASCII 数字

欢迎关注微.信.公..众.号: 爱写Bug

目录
相关文章
|
3月前
|
存储 算法 程序员
【Leetcode 程序员面试金典 01.01】判定字符是否唯一 —— 位运算|哈希表
可以使用哈希表或位运算来解决此问题:由题可知s[i]仅包含小写字母,int[26]即能表示字符的出现次数;
|
1月前
|
网络协议
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
|
2月前
|
存储 算法 Go
LeetCode 第三题: 无重复字符的最长子串
  给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
|
3月前
【Leetcode 2707】字符串中的额外字符 —— 动态规划
1. 状态定义:把`s[i−1]`当做是额外字符,`d[i] = d[i−1] + 1` 2. 状态转移方程:遍历所有的`j(j∈[0,i−1])`,如果子字符串`s[j...i−1]`存在于`dictionary`中,那么`d[i] = min d[j] 3. 初始状态`d[0] = 0`,最终答案为`d[n]`
|
3月前
leetcode:3. 无重复字符的最长子串
leetcode:3. 无重复字符的最长子串
16 0
|
3月前
|
算法
leetcode:387. 字符串中的第一个唯一字符
leetcode:387. 字符串中的第一个唯一字符
12 0
|
22天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3