当前位置:网站首页>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;
}
}
边栏推荐
- stm32F407------SPI
- 搜狗批量推送软件-搜狗批量推送工具【2022最新】
- 企业直播风起:目睹聚焦产品,微赞拥抱生态
- Roson的Qt之旅#99 QML表格控件-TableView
- postgreSQL 密码区分大小写 ,有参数控制吗?
- How to install govendor and open a project
- GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
- 2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
- 第三章、数据类型和变量
- 3D semantic segmentation - PVD
猜你喜欢
![[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

失意的互联网人拼命叩开Web3大门

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成

企业直播风起:目睹聚焦产品,微赞拥抱生态

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用

【目标检测】YOLOv5跑通VOC2007数据集(修复版)

In the eyes of 100 users, there are 100 QQS

Using rank to discuss the solution of linear equations / the positional relationship of three planes

【数学建模绘图系列教程】二、折线图的绘制与优化

霸榜COCO!DINO: 让目标检测拥抱Transformer
随机推荐
postgreSQL 密码区分大小写 ,有参数控制吗?
EasyUI DataGrid control uses
Headless mode of new selenium4.3 in egde browser
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Replicate swin on Huawei ascend910_ transformer
Who moved my memory and revealed the secret of 90% reduction in oom crash
接口自动化测试Postman+Newman+Jenkins
基于redis6.2.4的redis cluster部署
Go语言系列:Go从哪里来,Go将去哪里?
Outlook tutorial, how to search for calendar items in outlook?
用秩讨论线性方程组的解/三个平面的位置关系
备考过程中,这些“谣言”千万不要信!
WPF implements user avatar selector
[book club issue 13] +ffmpeg video capture function
WPF 实现用户头像选择器
Birui data joins Alibaba cloud polardb open source database community
Sogou batch push software - Sogou batch push tool [2022 latest]
7.依赖注入
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
【数学建模绘图系列教程】二、折线图的绘制与优化