当前位置:网站首页>03. Longest substring without repeated characters
03. Longest substring without repeated characters
2022-07-25 17:10:00 【User 5573316】
#03. Longest substring without repeating characters
difficulty : secondary
Given a string , Please find out that there are no duplicate characters in it Longest substrings The length of .
Example 1:
Input : s = "abcabcbb" Output : 3 explain : Because the longest substring without repeating characters is "abc", So its length is 3. Example 2:
Input : s = "bbbbb" Output : 1 explain : Because the longest substring without repeating characters is "b", So its length is 1. Example 3:
Input : s = "pwwkew" Output : 3 explain : Because the longest substring without repeating characters is "wke", So its length is 3. Please note that , Your answer must be Substring The length of ,"pwke" Is a subsequence , Not substring . Example 4:
Input : s = "" Output : 0
Tips :
0 <= s.length <= 5 * 104 s By the English letters 、 Numbers 、 Symbols and spaces
# queue
# Ideas
If there is repetition, it will be directly thrown out of the queue
# Code
class Solution {
public static int lengthOfLongestSubstring(String s) {
Queue<Character> queue = new LinkedList<>();
int length = 0;
for (char c:s.toCharArray()){
while (queue.contains(c)) {
queue.poll();
}
queue.add(c);
length = Math.max(length, queue.size());
}
return length;
}
}
# Set Method
# Ideas
To judge whether or not to repeat , If repeated, repeat the elements from set Remove... From collection , obtain set.size
# Code
class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0, right = 0, max = 0;
Set<Character> set = new HashSet<>();
while (right < s.length()) {
if (set.contains(s.charAt(right))) {
set.remove(s.charAt(left++));
} else {
set.add(s.charAt(right++));
max = Math.max(max, set.size());
}
}
return max;
}
}
边栏推荐
- Why 4everland is the best cloud computing platform for Web 3.0
- 华泰vip账户证券开户安全吗
- Don't believe these "rumors" in the process of preparing for the exam!
- 聊聊如何用 Redis 实现分布式锁?
- Mindoc makes mind map
- Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
- Enterprise live broadcast: witness focused products, praise and embrace ecology
- 大型仿人机器人的技术难点和应用情况
- ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
- [target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
猜你喜欢

Jenkins' role based authorization strategy installation configuration
![[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类

2D semantic segmentation -- deeplabv3plus reproduction

Briefly describe the implementation principle of redis cluster

Exception handling mechanism topic 1

MySQL linked table query, common functions, aggregate functions

Customize MVC project login registration and tree menu

What are the free low code development platforms?

Mindoc makes mind map
随机推荐
【目标检测】YOLOv5跑通VisDrone数据集
Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
华泰vip账户证券开户安全吗
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
[book club issue 13] +ffmpeg video capture function
[OBS] frame loss and frame priority before transmission
爬虫框架-crawler
超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
企业直播风起:目睹聚焦产品,微赞拥抱生态
3D semantic segmentation - PVD
EasyUI modification and DataGrid dialog form control use
7.依赖注入
01.两数之和
Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time
7. Dependency injection
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
Mindoc makes mind map
postgreSQL 密码区分大小写 ,有参数控制吗?