当前位置:网站首页>leecode3. 无重复字符的最长子串
leecode3. 无重复字符的最长子串
2022-07-07 10:55:00 【苗苗大佬】
class Solution:
def lengthOfLongestSubstring(s: str) -> int:
# 哈希集合,记录每个字符是否出现过
occ = set()
n = len(s)
# 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
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
# 第 i 到 rk 个字符是一个极长的无重复字符子串
ans = max(ans, rk - i + 1)
print(ans)
s=Solution.lengthOfLongestSubstring("pwwkew")
边栏推荐
- [learn micro services from 0] [02] move from single application to service
- Ip2long and long2ip analysis
- NPM instal reports agent or network problems
- mysql怎么创建,删除,查看索引?
- Grep of three swordsmen in text processing
- 《ASP.NET Core 6框架揭秘》样章[200页/5章]
- HZOJ #236. 递归实现组合型枚举
- Cookie
- HZOJ #236. Recursive implementation of combinatorial enumeration
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
猜你喜欢
Importance of database security
关于 appium 启动 app 后闪退的问题 - (已解决)
NPM instal reports agent or network problems
opencv的四个函数
Master formula. (used to calculate the time complexity of recursion.)
Connect to blog method, overload, recursion
ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集
Sorting, dichotomy
[statistical learning methods] learning notes - Chapter 5: Decision Tree
HZOJ #240. 图形打印四
随机推荐
How to reset Google browser? Google Chrome restore default settings?
飞桨EasyDL实操范例:工业零件划痕自动识别
【从 0 开始学微服务】【01】什么是微服务
Go语言学习笔记-结构体(Struct)
【无标题】
Day-19 IO stream
HZOJ #236. Recursive implementation of combinatorial enumeration
DHCP 动态主机设置协议 分析
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
有什么类方法或是函数可以查看某个项目的Laravel版本的?
“新红旗杯”桌面应用创意大赛2022
Differences between MySQL storage engine MyISAM and InnoDB
图形对象的创建与赋值
2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
Polymorphism, final, etc
Common text processing tools
红杉中国完成新一期90亿美元基金募集
How to reset Firefox browser
@Resource和@Autowired的区别?
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘