class Solution:
def lengthOfLongestSubstring(self, 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)
return ans
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:return 0
left = 0
lookup = set()
n = len(s)
max_len = 0
cur_len = 0
for i in range(n):
cur_len += 1
while s[i] in lookup:
lookup.remove(s[left])
left += 1
cur_len -= 1
if cur_len > max_len:max_len = cur_len
lookup.add(s[i])
return max_len
python1中
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
python2中的
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s : return 0
left = 0
lookup = set()
n = len(s)
max = 0
cur = 0
for i in range(n):
cur += 1
while s[i] in lookup:
lookup.remove(s[left])
left += 1
cur -= 1
if cur > max :
max = cur
lookup.add(s[i])
return max