当前位置:网站首页>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")
边栏推荐
- 【从 0 开始学微服务】【02】从单体应用走向服务化
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
- How to apply @transactional transaction annotation to perfection?
- Go语言学习笔记-结构体(Struct)
- HZOJ #236. 递归实现组合型枚举
- How to reset Firefox browser
- 详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
- visual stdio 2017关于opencv4.1的环境配置
- ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
- 认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
猜你喜欢
认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
红杉中国完成新一期90亿美元基金募集
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
How to apply @transactional transaction annotation to perfection?
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
Day22 deadlock, thread communication, singleton mode
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
[pytorch practice] use pytorch to realize image style migration based on neural network
图像像素读写操作
随机推荐
图形对象的创建与赋值
认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
Day-19 IO stream
Day-24 UDP, regular expression
Steps of building SSM framework
3D content generation based on nerf
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
MySQL导入SQL文件及常用命令
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
HZOJ #240. Graphic printing IV
What are the benefits of ip2long?
HZOJ #240. 图形打印四
[pytorch practice] write poetry with RNN
Leetcode brush question: binary tree 24 (the nearest common ancestor of binary tree)
HZOJ #236. 递归实现组合型枚举
What if the xshell evaluation period has expired
SSM框架搭建的步骤
ip2long之后有什么好处?
Common text processing tools