当前位置:网站首页>Leecode3. Longest substring without repeated characters
Leecode3. Longest substring without repeated characters
2022-07-07 13:33:00 【Miaomiao boss】
class Solution:
def lengthOfLongestSubstring(s: str) -> int:
# Hash set , Record whether each character appears
occ = set()
n = len(s)
# Right pointer , The initial value is -1, It's like we're on the left side of the left bound of the string , It's not moving yet
rk, ans = -1, 0
for i in range(n):
if i != 0:
# The left pointer moves one space to the right , Remove a character
occ.remove(s[i - 1])
while rk + 1 < n and s[rk + 1] not in occ:
# Keep moving the right pointer
occ.add(s[rk + 1])
rk += 1
# The first i To rk A character is a very long non repeating character substring
ans = max(ans, rk - i + 1)
print(ans)
s=Solution.lengthOfLongestSubstring("pwwkew")
边栏推荐
猜你喜欢
Realize the IP address home display function and number home query
Ogre入门尝鲜
作战图鉴:12大场景详述容器安全建设要求
存储过程的介绍与基本使用
Write it down once Net a new energy system thread surge analysis
Cmake learning and use notes (1)
2022-7-7 Leetcode 844.比较含退格的字符串
xshell连接服务器把密钥登陆改为密码登陆
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
随机推荐
Navicat run SQL file import data incomplete or import failed
信号强度(RSSI)知识整理
作战图鉴:12大场景详述容器安全建设要求
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
MongoDB内部的存储原理
MATLAB中polarscatter函数使用
单片机学习笔记之点亮led 灯
分布式事务解决方案
Ikvm of toolbox Net project new progress
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
Problems that cannot be accessed in MySQL LAN
648. 单词替换 : 字典树的经典运用
How far can it go to adopt a cow by selling the concept to the market?
xshell连接服务器把密钥登陆改为密码登陆
Clion mingw64 Chinese garbled code
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
How did Guotai Junan Securities open an account? Is it safe to open an account?
[learning notes] agc010
What parameters need to be reconfigured to replace the new radar of ROS robot
Milkdown 控件图标