当前位置:网站首页>The longest substring that cannot have repeating characters in a leetcode/substring
The longest substring that cannot have repeating characters in a leetcode/substring
2022-08-03 23:21:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 016. 不含重复字符的最长子字符串 * Substrings cannot have repeating characters * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长连续子字符串 的长度. */
public class Solution16 {
/** * 双指针+set散列表 * 由set是否包含字符,Determines the right movement of the left and right pointers * set<Character> set.contains()Whether to contain repeating characters * 不contains repeating characters right右移 * contains repeating characters 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++) {
// 不contains repeating characters The right pointer moves all the way to the right
while (i < s.length() && !set.contains(s.charAt(i))) {
set.add(s.charAt(i));
i++;
}
// contains repeating characters The left pointer moves all the way to the right(Try to remove the repeated character)
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)
边栏推荐
- 响应式织梦模板塑身瑜伽类网站
- OPC UA 与IEC61499 深度融合(1)
- 【职场杂谈】售前与销售工作配合探讨
- Creo 9.0在草图环境中创建坐标系
- Canvas App中点击图标生成PDF并保存到Dataverse中
- Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
- Flutter教程之为什么 Flutter 是创业的最佳选择?
- P1996 约瑟夫问题
- golang写的存储引擎,基于b+树,mmap
- .NET6之MiniAPI(十四):跨域CORS(上)
猜你喜欢
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
完全二叉树问题
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
HCIP BGP lab report
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time
Creo 9.0二维草图的诊断:着色封闭环
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
redis持久化方式
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
随机推荐
Redis persistence method
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
Kotlin - extension functions and operator overloading
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
Take an example of a web worker
Deep integration of OPC UA and IEC61499 (1)
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
Creo 9.0二维草图的诊断:加亮开放端点
1067 Sort with Swap(0, i)
完全二叉树问题
工作小计 QT打包
如何创建一个Web项目
软测人每个阶段的薪资待遇,快来康康你能拿多少?
射频芯片(RFIC)的协议之5G及其调制
log4j-slf4j-impl cannot be present with log4j-to-slf4j
Scala基础【正则表达式、框架式开发原则】
jav一键生成数据库文档
node连接mysql数据库报错:Client does not support authentication protocol requested by server
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve