当前位置:网站首页>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)
边栏推荐
- CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
- Golang Chapter 2: Program Structure
- [Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- 密码学基础以及完整加密通讯过程解析
- MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- 最小化安装debian11
- AOSP CameraLatencyHistogram的原理与使用
- Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
- RPA power business automation super order!
猜你喜欢

Scala基础【正则表达式、框架式开发原则】

密码学基础以及完整加密通讯过程解析

禾匠编译错误记录

【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。

Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting

Canvas App中点击图标生成PDF并保存到Dataverse中

设置工作模式与环境(下):探查和收集信息

ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例

Minimized installation of debian11

Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
随机推荐
Unity2021发布WebGL雾效消失问题
设置工作模式与环境(下):探查和收集信息
响应式织梦模板除尘器类网站
射频芯片ATE测试从入门到放弃之参数测试
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
HCIP BGP实验报告
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
3D Semantic Segmentation - 2DPASS
(PC+WAP)织梦模板不锈钢类网站
UVa 1025 - A Spy in the Metro (White Book)
How many way of calling a function?
utlis thread pool
log4j-slf4j-impl cannot be present with log4j-to-slf4j
V8中的快慢数组(附源码、图文更易理解)
End-to-End Lane Marker Detection via Row-wise Classification
1067 Sort with Swap(0, i)
complete binary tree problem
全球首款量产,获定点最多!这家AVP Tier1如何实现领跑?
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
七夕?new一个对象