😐 LeetCode 3. 无重复字符的最长子串 [指针]

170 words
1 minute
😐 LeetCode 3. 无重复字符的最长子串 [指针]

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

示例 1:

输入: s = "abcabcbb"
输出: 3,即 length(abc)

示例 2:

输入: s = "bbbbb"
输出: `1“

示例 2:

输入: s = "pwwkew"
输出: 3,即 length(wke)

思路#

  • 定义一个空数组用于保存索引
  • 通过右指针遍历字符串,将字符取出来
  • 若字符不在数组里,则不重复,此时左指针不动,计算长度,然后更新索引
  • 若字符在数组里,则更新左指针到 right + 1

题解#

class Solution:
    @staticmethod
    def maxLenSubstr(s):
        index = [0] * 128
        maxLen, left = 0
        for right in range(len(s)):
            ch = s[right]
# 遇到重复字符时 left 才会变
            left = max(left, index[ord(ch)])
            maxLen = max(maxLen, right - left + 1)
            index[ord(ch)] = right + 1
        return maxLen

Comments

Profile Image of the Author
永雏多氢菲
∴さて····どこへ行こうか?
公告
随缘分享喵
Music
Cover

Music

No playing

0:00 0:00
No lyrics available
Categories
Tags
Site Statistics
Posts
144
Categories
6
Tags
9
Total Words
2,255,454
Running Days
0 days
Last Activity
0 days ago

Table of Contents