当前位置:网站首页>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;
}
}
边栏推荐
- ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
- HCIP笔记十二天
- How to delete Microsoft Pinyin input method in win10
- WPF 实现用户头像选择器
- Rosen's QT journey 99 QML table control tableview
- 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毕业季丨毕业不忘初心 荣耀再上征程
- Virtual memory management
- Add batch delete
- Jenkins' role based authorization strategy installation configuration
猜你喜欢

2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis

第三章、数据类型和变量

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

动态规划题目记录

在华为昇腾Ascend910上复现swin_transformer

【obs】转载:OBS直播严重延迟和卡顿怎么办?

Briefly describe the implementation principle of redis cluster

Redis cluster deployment based on redis6.2.4

简述redis集群的实现原理

3D semantic segmentation - scribed supervised lidar semantic segmentation
随机推荐
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
win10自带的框选截图快捷键
Don't believe these "rumors" in the process of preparing for the exam!
Jenkins' role based authorization strategy installation configuration
Box selection screenshot shortcut key of win10
Rosen's QT journey 99 QML table control tableview
C # introductory basic tutorial
超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
Hcip notes 12 days
【南京航空航天大学】考研初试复试资料分享
Random talk on generation diffusion model: DDPM = Bayesian + denoising
Add batch delete
搜狗批量推送软件-搜狗批量推送工具【2022最新】
How to deploy applications on IPFs using 4everland cli
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
【redis】redis安装
Hcip notes 11 days
第三章、数据类型和变量