LeetCode 笔记二 寻找无重复最长子串

Longest Substring Without Repeating Characters

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

Example

example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.

example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.

example 3:

Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

Code

一开始依旧想当然的就啪啪啪开始码代码,大概思路就是,用一个flag标记下一次的起始位置,count标记每次不重复的值,longest用来表示最后返回的最大字串值。当第i个字符出现在当前字串的时候,flag加一,取longest跟count的最大值,count回到一,进行下一个子串的搜索:

class Solution:
	def lengthOfLongestSubstring(self, s: str) -> int:
		count = 1
		longest = 1
		flag = 0
		for i in range(1, len(s)):
			if s[i] in s[flag:i]:
				longest = max(count, longest)
				count = 1
				flag += 1
			else:
				count += 1
			longest = max(count, longest)
		return longest

结果也是妥妥的报错了:

Input: 'dvdf'
Output: 2
Expected: 3

咋回事呢?后来发现原来是循环不对,当循环发现索引为2的’d’在前面的子串时,循环直接从最后的’f’开始了。于是开始对循环进行改进,改了很多遍,出现了很多不同的问题,比如字符串为空时应该输出0,所以在一开始加了一句返回0的语句。修改了很多遍之后,变成以下版本:

class Solution:
	def lengthOfLongestSubstring(self, s: str) -> int:
		if s == '':
			return 0

		i = 0
		longest = 1
		while i < len(s):
			count = 1
			flag = i
            for j in range(i+1, len(s)):
                if j == '':
                    break
                if s[j] in s[flag:j]:
                    longest = max(longest, count)
                    count = 1
                    flag += 1
                else:
                    count += 1
                if j == len(s) - 1:
                    return max(longest, count)
            longest = max(longest, count)
            i += 1
			return longest

这个看着逻辑也很没有问题了,结果一提交,gg报错了,报错信息跟上一段代码一样,不过这里面发生错误的原因还是相差很大的:

Input: 'dvdf'
Output: 2
Expected: 3

又把代码捋了一遍,发现是因为当循环发现当前字符在子串内时,没有返回初始遍历状态,这时候j走到了字符串最后一个值,于是return了。最后改成:

class Solution:
	def lengthOfLongestSubstring(self, s: str) -> int:
		if s == '':
			return 0

		i = 0
		longest = 1
		while i < len(s):
			count = 1
			flag = i
            for j in range(i+1, len(s)):
                if j == '':
                    break
                if s[j] in s[flag:j]:
                    longest = max(longest, count)
                    count = 1
                    flag += 1
                    break
                else:
                    count += 1
                if j == len(s) - 1:
                    return max(longest, count)
            longest = max(longest, count)
            i += 1
			return longest

代码运行成功啦!不过效果并不是很好:

987 / 987 test cases passed.
Runtime: 836 ms
Memory Usage: 14.1 MB

Runtime: 836 ms, faster than 6.82% of Python3 online submissions for Longest Substring Without Repeating Characters.
Memory Usage: 14.1 MB, less than 5.10% of Python3 online submissions for Longest Substring Without Repeating Characters.

本着学习的心态,我又去看了一下solution里的人写的,发现可以用字典来搞(第一个问题也是用了字典,字典可真好用!以后优先用字典好了…):

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        
        Dict = {}
        longest = 0
        flag = 0
        count = 0
        for i, c in enumerate(s):
            if (c in Dict) and (Dict[c] >= flag):
                longest = max(longest, count)
                flag = Dict[c] + 1
                count = i - Dict[c]
            else:
                count += 1
            Dict[c] = i
        return max(longest, count)

结果:

987 / 987 test cases passed.
Runtime: 56 ms
Memory Usage: 14.1 MB

Runtime: 56 ms, faster than 92.70% of Python3 online submissions for Longest Substring Without Repeating Characters.
Memory Usage: 14.1 MB, less than 5.10% of Python3 online submissions for Longest Substring Without Repeating Characters.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值