当前位置:网站首页>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;
}

方法二:滑动窗口及优化


边栏推荐
- RequestContextHolder
- 荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
- Kotlin-Flow common encapsulation class: the use of StateFlow
- Shell conditional statement judgment
- 私域流量时代来临,电商企业如何布局?
- 内部类、static关键字、final
- Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
- rosbag工具plotjuggler无法打开rosbag的问题
- 数据库基本概述与SQL概述
- 普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
猜你喜欢

【Harmony OS】【ARK UI】ETS 上下文基本操作

Shell之条件语句

如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果

【Harmony OS】【ARK UI】Date 基本操作

Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures

typescript46-函数之间的类型兼容性

IO process thread -> thread -> day5

Super handy drawing tool is recommended

OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述

Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
随机推荐
建立树形结构
WinForm的控件二次开发
[Harmony OS] [ARK UI] ETS context basic operations
在线密码生成工具推荐
Get the Ip tool class
超好用的画图工具推荐
Flink state
User password verification
4.深度学习的几何解释与梯度的优化
DFS's complement to pruning
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
js中的闭包
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
接口管理工具YApi怎么用?颜值高、易管理、超好用
Apache DolphinScheduler版本2.0.5分布式集群的安装
typescript42-readonly修饰符
MySQL 出现 The table is full 的解决方法
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?