当前位置:网站首页>LeetCode #3.无重复字符的最长子串
LeetCode #3.无重复字符的最长子串
2022-07-29 05:24:00 【张楚明ZCM】
题目截图

方法一:暴力法
利用列表。利用列表切片来滑动窗口。
遍历字符串s,不断将字符串的字符添加到列表中。
如果发现字符串中新的元素在前面的列表中有,立刻使用切片,使切片的开头切在重复元素的后一位,然后添加那个重复的元素(此时已经把重复的部分切掉了),再继续往下遍历。
每一次添加都比较长度,选择长度大的那个,最后返回最大子串长度。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
a = []
res = 0
for i in s:
if i in a:
a = a[a.index(i)+1:]
a.append(i)
res=res if len(a)< res else len(a)
return resres可以直接用max()函数
res = max(res, len(a))完整测试代码
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
a = []
res = 0
for i in s:
if i in a:
a = a[a.index(i)+1:]
a.append(i)
res=res if len(a)< res else len(a)
return res
class main():
a = Solution()
s = "abcabcbb"
print(a.lengthOfLongestSubstring(s))
if __name__ == '__main__':
main()方法二:滑动窗口一
set()函数可以创建一个不重复的元素集,可以利用该函数判断是否有重复的部分。
内循环判断新添加的元素是否重复,如果不重复则指针右移,将其加入元素集。如果新加入的元素重复在无法进入循环,跳出循环后进外循环,外循环会删除掉前面的元素,该元素为之前重复的元素。
这样利用左右指针滑动窗口可以找到一系列子字符串,利用max()函数返回无重复最大子字符串。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# 哈希集合,记录每个字符是否出现过
occ = set()
n = len(s)
rk, ans = -1, 0
for i in range(n):
if i != 0:
occ.remove(s[i - 1])
while rk + 1 < n and s[rk + 1] not in occ:
occ.add(s[rk + 1])
rk += 1
ans = max(ans, rk - i + 1)
return ans方法三:滑动窗口二
基本思路与上面相同有一点点区别。更容易看懂一点。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
occ = set()
left = 0
max_len, cur_len = 0, 0
n = len(s)
for i in range(n):
cur_len += 1
while s[i] in occ:
occ.remove(s[left])
left += 1
cur_len -= 1
max_len = max(max_len, cur_len)
occ.add(s[i])
return max_len边栏推荐
- 2022暑初二信息竞赛学习成果分享2
- crawl笔记
- Huawei cloud 14 day Hongmeng device development -day2 compilation framework
- Reading papers on false news detection (5): a semi supervised learning method for fake news detection in social media
- NoClassDefFoundError 处理
- 太原市公交路线爬取
- 【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
- Pytorch's data reading mechanism
- STM32FF030 替代国产单片机——DP32G030
- QT learning notes QtSql
猜你喜欢

TB6600+stm32F407测试

基于wifi的温度采集与控制系统

基于AD9850的多功能信号发生器

SimpleFOC调参1-力矩控制

Pytorch's data reading mechanism

【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?

抽象封装继承多态

Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)

充电桩充电技术新能源充电桩开发

【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决
随机推荐
动态加载数据
智慧能源管理系统解决方案
简洁代码实现pdf转word文档
Ml6 self study notes
一些工具,插件,软件链接分享给大家~
2022 spring recruit - Shanghai an road FPGA post Manager (and Lexin SOC interview)
HAL库学习笔记-10 HAL库外设驱动框架概述
利用云打码来破解登录遇到验证码的问题
STM32 串口乱码
噪音监测传感系统
JUC并发知识点
FPGA based: moving target detection (supplementary simulation results, available)
Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
DP4301—SUB-1G高集成度无线收发芯片
scanBasePackages扫包范围配置
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
QT learning notes QT model/view
HAL库学习笔记- 8 串口通信之概念
EPS32+Platform+Arduino 跑马灯