当前位置:网站首页>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;
}
}
边栏推荐
- 用秩讨论线性方程组的解/三个平面的位置关系
- [mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
- Hcip notes 12 days
- Multi tenant software development architecture
- 我们被一个 kong 的性能 bug 折腾了一个通宵
- [MySQL] takes you to the database
- Box selection screenshot shortcut key of win10
- [Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- Add batch delete
- 无聊发文吐槽工作生活
猜你喜欢
随机推荐
月薪1万在中国是什么水平?答案揭露残酷的收入真相
大型仿人机器人的技术难点和应用情况
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
win10如何删除微软拼音输入法
Box selection screenshot shortcut key of win10
2D semantic segmentation -- deeplabv3plus reproduction
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 的最佳云计算平台
异常处理机制专题1
[target detection] yolov5 Runtong voc2007 dataset (repair version)
[book club issue 13] +ffmpeg video capture function
Enumeration classes and magic values
Lvgl 7.11 tileview interface cycle switching
Talk about how to use redis to realize distributed locks?
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
postgreSQL 密码区分大小写 ,有参数控制吗?
Go语言系列:Go从哪里来,Go将去哪里?
jenkins的文件参数,可以用来上传文件
Rebudget汇报PPT
How to delete Microsoft Pinyin input method in win10









