当前位置:网站首页>leetcode/子串中不能有重复字符的最长子串
leetcode/子串中不能有重复字符的最长子串
2022-08-03 23:20:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 016. 不含重复字符的最长子字符串 * 子串中不能有重复字符 * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长连续子字符串 的长度。 */
public class Solution16 {
/** * 双指针+set散列表 * 由set是否包含字符,决定左右指针的右移 * set<Character> set.contains()是否包含重复字符 * 不包含重复字符 right右移 * 包含重复字符 left右移 */
public int lengthOfLongestSubstring1(String s) {
Set<Character> set = new HashSet<>(s.length());
int i = 0;
int maxLen = 0;
for (int j = 0; j < s.length(); j++) {
// 不包含重复字符 右指针一直右移
while (i < s.length() && !set.contains(s.charAt(i))) {
set.add(s.charAt(i));
i++;
}
// 包含重复字符 左指针一直右移(尝试把重复的那个字符移除掉)
set.remove(s.charAt(j));
maxLen = Math.max(maxLen, i - j);
}
return maxLen;
}
public static void main(String[] args) {
Solution16 solution16 = new Solution16();
System.out.println(solution16.lengthOfLongestSubstring1("abc"));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/wtcaE1/solution/bu-han-zhong-fu-zi-fu-de-zui-chang-zi-zi-4g7m/
来源:力扣(LeetCode)
边栏推荐
- 【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
- Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
- 生成器版和查看器版有什么区别?
- SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
- End-to-End Lane Marker Detection via Row-wise Classification
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- Cloud platform construction solutions
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- Canvas App中点击图标生成PDF并保存到Dataverse中
- 【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
猜你喜欢

Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting

Binary search tree to solve the fallen leaves problem

【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve

BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记

End-to-End Lane Marker Detection via Row-wise Classification

Zilliz 2023 秋季校园招聘正式启动!

.NET6之MiniAPI(十四):跨域CORS(上)

静态文件快速建站

直播预告 | 构建业务智联,快速拥抱财务数字化转型

Creo 9.0二维草图的诊断:加亮开放端点
随机推荐
Zilliz 2023 秋季校园招聘正式启动!
Creo 9.0在草图环境中创建坐标系
软测人每个阶段的薪资待遇,快来康康你能拿多少?
物联网新零售模式,引领购物新潮流
Quickly build a website with static files
用两个栈模拟队列
Testng监听器
UVa 1025 - A Spy in the Metro (White Book)
【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
用两个栈模拟队列
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
log4j-slf4j-impl cannot be present with log4j-to-slf4j
With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
软件测试内卷严重,如何提升自己的竞争力呢?
【day6】类与对象、封装、构造方法
What is the difference between the generator version and the viewer version?
代码随想录笔记_动态规划_416分割等和子集
Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
数据分析知识点搜集(纯粹的搜集)
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.