当前位置:网站首页>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")
边栏推荐
- 【无标题】
- Leetcode skimming: binary tree 20 (search in binary search tree)
- test
- Four functions of opencv
- 【无标题】
- DHCP 动态主机设置协议 分析
- .Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
- [statistical learning methods] learning notes - Chapter 4: naive Bayesian method
- Day-24 UDP, regular expression
- Conversion from non partitioned table to partitioned table and precautions
猜你喜欢

Go语言学习笔记-结构体(Struct)

高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作
![[statistical learning method] learning notes - support vector machine (Part 2)](/img/bc/bb4f809ff434fabc10f8e97f592fa7.png)
[statistical learning method] learning notes - support vector machine (Part 2)

聊聊Redis缓存4种集群方案、及优缺点对比

- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net

Connect to blog method, overload, recursion

2022 polymerization process test question simulation test question bank and online simulation test
![[statistical learning methods] learning notes - Chapter 5: Decision Tree](/img/0e/c60e04ab4a7ae4728cc76eff1c028a.png)
[statistical learning methods] learning notes - Chapter 5: Decision Tree

2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation

Talk about four cluster schemes of redis cache, and their advantages and disadvantages
随机推荐
File operation command
Find ID value MySQL in string
Layer pop-up layer closing problem
达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
ip2long与long2IP 分析
Sed of three swordsmen in text processing
test
What if the xshell evaluation period has expired
【从 0 开始学微服务】【00】课程概述
What are the benefits of ip2long?
Day26 IP query items
Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
Day-16 set
货物摆放问题
What kind of methods or functions can you view the laravel version of a project?
3D content generation based on nerf
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
Day-14 common APIs
Conversion from non partitioned table to partitioned table and precautions
How to continue after handling chain interruption / sub chain error removed from scheduling