当前位置:网站首页>3. 无重复字符的最长子串
3. 无重复字符的最长子串
2022-08-03 05:09:00 【破烂摆烂人】
3. 无重复字符的最长子串

方法一:滑动窗口


public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<Character>();/*哈希集合-记录字符是否重复*/
int len = s.length();/*数组长度*/
int ans = 0;/*记录最大长度*/
int right = 0;/*右指针*/
int left = 0;/*左指针*/
while (left<len){
while (right < len && !set.contains(s.charAt(right))) {
/*右指针右移条件*/
set.add(s.charAt(right));
right++;
}
ans = Math.max(ans, right-left);/*更新最大长度*/
set.remove(s.charAt(left++));/*集合移除最左的元素-左指针右移*/
}
return ans;
}

方法二:滑动窗口及优化


边栏推荐
- Apache DolphinScheduler版本2.0.5分布式集群的安装
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- Shell conditional statement judgment
- GIS数据漫谈(六)— 投影坐标系统
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- 接口测试如何准备测试数据
- 2022/08/02 Study Notes (day22) Multithreading
- typescript39-class类的可见修饰符
- Technology Sharing | How to do assertion verification for xml format in interface automation testing?
- IO进程线程->线程->day5
猜你喜欢
随机推荐
Exception(异常) 和 Error(错误)区别解析
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
接口测试框架实战(四)| 搞定 Schema 断言
Create a tree structure
Shell conditional statement judgment
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
测试人员的价值体现在哪里
获取Ip工具类
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
社交电商如何做粉丝运营?云平台怎么选择商业模式?
GIS数据漫谈(六)— 投影坐标系统
【Harmony OS】【ARK UI】轻量级数据存储
DDL操作数据库、表、列
Flink状态
typescript44-对象之间的类兼容器
VR全景展打造专属元宇宙观展空间
探索性测试的概念及方法
Interface test practice | Detailed explanation of the difference between GET / POST requests
表的创建、修改与删除









