当前位置:网站首页>Leetcode 3. longest substring without repeated characters
Leetcode 3. longest substring without repeated characters
2022-07-29 06:23:00 【Zhangchuming ZCM】
Power button | 3. Longest substring without repeating characters
Title screenshot
Method 1 : Violence law
Use the list . Use list slices to slide windows .
Traversal string s, Constantly add the characters of the string to the list .
If a new element in the string is found in the previous list , Use slices now , Cut the beginning of the slice to the last bit of the repeating element , Then add the repeating element ( At this time, the repeated part has been cut off ), Then continue to traverse down .
Each addition compares the length , Choose the longer one , Finally, the maximum substring length is returned .
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
a = []
res = 0
for i in s:
if i in a:
a = a[a.index(i)+1:]
a.append(i)
res=res if len(a)< res else len(a)
return res
res It can be used directly max() function
res = max(res, len(a))
Complete test code
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
a = []
res = 0
for i in s:
if i in a:
a = a[a.index(i)+1:]
a.append(i)
res=res if len(a)< res else len(a)
return res
class main():
a = Solution()
s = "abcabcbb"
print(a.lengthOfLongestSubstring(s))
if __name__ == '__main__':
main()
Method 2 : Slide window one
set() Function can create a set of non repeating elements , This function can be used to determine whether there is a repeated part .
Python set() function | Novice tutorial
The inner loop determines whether the newly added element is repeated , If it does not repeat, the pointer moves to the right , Add it to the element set . If the newly added element repeats and cannot enter the loop , Jump out of the loop and enter the outer loop , The outer loop will delete the previous elements , This element is a previously repeated element .
In this way, a series of substrings can be found by using the left and right pointer sliding window , utilize max() Function returns the largest substring without repetition .
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# Hash set , Record whether each character appears
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
Method 3 : Sliding window II
The basic idea is the same as the above, with a little difference . It's easier to understand .
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
occ = set()
left = 0
max_len, cur_len = 0, 0
n = len(s)
for i in range(n):
cur_len += 1
while s[i] in occ:
occ.remove(s[left])
left += 1
cur_len -= 1
max_len = max(max_len, cur_len)
occ.add(s[i])
return max_len
边栏推荐
猜你喜欢
多线程和并发
[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
Logistic regression - project practice - credit card detection task (Part 2)
Leetcode 26. delete duplicates in the ordered array
循环链表和双向链表
LeetCode #7.整数反转
Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
随机推荐
Computer factory interview questions
LeetCode #876.链表的中间结点
Abstract classes and interfaces
Ml self study notes 5
网络安全学习篇
【Leetcode刷题】数组1——双指针
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
Multithreading and concurrency
唯美girls
Ml8 self study notes LDA principle formula derivation
#7110 数字走向2 题解
UE5 纹理系统讲解及常见问题设置及解决方案
JUC concurrent knowledge points
scanBasePackages扫包范围配置
关于【链式前向星】的自学理解
STM32 printf问题总结 semihosting microLIB理解
Install MySQL from scratch (MySQL installation document - unzipped version)
leetcode---技巧
循环链表和双向链表
Encapsulation - Super keyword