当前位置:网站首页>03.无重复字符的最长子串
03.无重复字符的最长子串
2022-07-25 17:07:00 【用户5573316】
#03.无重复字符的最长子串
难度:中等
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2:
输入: s = "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3:
输入: s = "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 示例 4:
输入: s = "" 输出: 0
提示:
0 <= s.length <= 5 * 104 s 由英文字母、数字、符号和空格组成
# 队列
# 思路
有重复则直接抛出队列
# 代码
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方法
# 思路
判断是否重复,重复的话将重复元素从set集合中移除,获取set.size
# 代码
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;
}
}
边栏推荐
- 一百个用户眼中,就有一百个QQ
- The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
- C # introductory basic tutorial
- Chapter III data types and variables
- Rainbow plug-in extension: monitor MySQL based on MySQL exporter
- What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
- Hcip notes 12 days
- 【数学建模绘图系列教程】二、折线图的绘制与优化
- Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
- 2D semantic segmentation -- deeplabv3plus reproduction
猜你喜欢

备考过程中,这些“谣言”千万不要信!

Automatic reply of wechat official account development message

Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
![[target detection] yolov5 Runtong voc2007 dataset (repair version)](/img/b6/b74e93ca5e1986e0265c58f750dce3.png)
[target detection] yolov5 Runtong voc2007 dataset (repair version)

数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?

How to install govendor and open a project

HCIP笔记十二天

China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot

复旦大学EMBA2022毕业季丨毕业不忘初心 荣耀再上征程

【redis】redis安装
随机推荐
为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
爬虫框架-crawler
失意的互联网人拼命叩开Web3大门
Multi tenant software development architecture
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
第六章 继承
【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测
How to install govendor and open a project
[cloud co creation] explore how gaussdb helps ICBC create core financial data
Use huggingface to quickly load pre training models and datasets in moment pool cloud
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
第四章:操作符
【南京航空航天大学】考研初试复试资料分享
QT listview list display component notes
mindoc制作思维导图
Talk about how to use redis to realize distributed locks?
What are the free low code development platforms?
Sogou batch push software - Sogou batch push tool [2022 latest]
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>