当前位置:网站首页>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;
}
}
边栏推荐
- Automatic reply of wechat official account development message
- 大型仿人机器人的技术难点和应用情况
- Who moved my memory and revealed the secret of 90% reduction in oom crash
- From digitalization to intelligent operation and maintenance: what are the values and challenges?
- Slf4j and log4j2 process logs
- Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
- [mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
- 博云容器云、DevOps平台斩获可信云“技术最佳实践奖”
- Chapter 4: operators
- Rainbow plug-in extension: monitor MySQL based on MySQL exporter
猜你喜欢

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装

Enterprise live broadcast: witness focused products, praise and embrace ecology

用秩讨论线性方程组的解/三个平面的位置关系

Jenkins' file parameters can be used to upload files

How to install govendor and open a project

异常处理机制专题1

7. Dependency injection

Birui data joins Alibaba cloud polardb open source database community

Rainbond插件扩展:基于Mysql-Exporter监控Mysql

失意的互联网人拼命叩开Web3大门
随机推荐
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
Is it safe to open a securities account in Huatai VIP account
stm32F407------SPI
Outlook tutorial, how to search for calendar items in outlook?
MySQL linked table query, common functions, aggregate functions
复旦大学EMBA2022毕业季丨毕业不忘初心 荣耀再上征程
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
Roson的Qt之旅#99 QML表格控件-TableView
搜狗批量推送软件-搜狗批量推送工具【2022最新】
Why 4everland is the best cloud computing platform for Web 3.0
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
异常处理机制专题1
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成
Go language series: where does go come from and where will go?
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
动态规划题目记录
Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time
win10设备管理认不到GTX1080Ti 显示设备的解决办法
Add batch delete
Fudan University EMBA peer topic: always put the value of consumers in the most important position