当前位置:网站首页>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边栏推荐
- 【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
- 滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
- Encapsulation - Super keyword
- 【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
- Sqlyog installation and configuration tutorial
- JUC concurrent knowledge points
- 官方教程 Redshift 09 Camera
- Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
- Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem
- 官方教程 Redshift 08 Light
猜你喜欢
随机推荐
网络安全学习篇
Logistic regression - project practice - credit card detection task (Part 2)
Ml4 self study notes
STM32 printf问题总结 semihosting microLIB理解
Leetcode 7. integer inversion
循环链表和双向链表
LeetCode #14. 最长公共前缀
动态加载数据
IDEA安装scala
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
LeetCode #876.链表的中间结点
爬取表情包
Ml8 self study notes LDA principle formula derivation
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
2022暑初二信息竞赛学习成果分享2
scanBasePackages扫包范围配置
Eight sorts ----------- bubble sort
ML10 self study notes SVM
Leetcode 26. delete duplicates in the ordered array









