前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 3: 无重复字符的最长子串

LeetCode 3: 无重复字符的最长子串

作者头像
爱写bug
发布2019-12-02 19:52:43
4700
发布2019-12-02 19:52:43
举报
文章被收录于专栏:爱写Bug爱写Bug

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

题目:

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

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

示例 1:

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

示例 2:

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

示例 3:

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

解题思路:

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

哈希映射:

Java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 数字

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 爱写Bug 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
  • 题目:
  • 解题思路:
  • 哈希映射:
  • 字符映射:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档