当前位置:网站首页>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 resres 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 ansMethod 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边栏推荐
- Huawei cloud 14 day Hongmeng device development -day7wifi function development
- 简洁代码实现pdf转word文档
- ML7 self study notes
- Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
- UE5 纹理系统讲解及常见问题设置及解决方案
- Computer factory interview questions
- Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
- 链表--------------------尾插法
- 顺序表和链表
- LeetCode #167.两数之和 II - 输入有序数组
猜你喜欢

scanBasePackages扫包范围配置

【软件工程之美 - 专栏笔记】29 | 自动化测试:如何把Bug杀死在摇篮里?

Leetcode 26. delete duplicates in the ordered array

Add time series index to two-dimensional table

Open source based on STM32: MHD Bluetooth speaker (including source code +pcb)

SQLyog 安装和配置教程
![寒假集训总结 (1.23~1.28) [第一梯队]](/img/cf/2f86ecc23bfe6d96ad0429c785663a.png)
寒假集训总结 (1.23~1.28) [第一梯队]

MySQL interview questions

Dynamic planning summary
![[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?](/img/e2/56234084d0cfad6906f9e84212182a.png)
[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?
随机推荐
Based on stc51: schematic diagram and source code of four axis flight control open source project (entry-level DIY)
Zero basics FPGA (5): counter of sequential logic circuit design (with introduction to breathing lamp experiment and simple combinational logic design)
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
QT learning notes - Import and export of Excel
Leetcode 7. integer inversion
抽象封装继承多态
【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?
Ml8 self study notes
从头安装MYSQL(MYSQL安装文档-解压版)
Leetcode 283. move zero
Abstract classes and interfaces
Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
LeetCode #26.删除有序数组中的重复项
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
LeetCode #14. 最长公共前缀
【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?