当前位置:网站首页>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;
}
方法二:滑动窗口及优化
边栏推荐
猜你喜欢
unity2D横板游戏教程6-敌人AI以及受击动画
Harmony OS Date ano UI 】 【 】 the basic operation
2022/08/02 Study Notes (day22) Multithreading
在线密码生成工具推荐
MySQL 入门:Case 语句很好用
MOSN 反向通道详解
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
【Harmony OS】【ARK UI】Date 基本操作
随机推荐
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
接口管理工具YApi怎么用?颜值高、易管理、超好用
2022/08/02 Study Notes (day22) Multithreading
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
软件开发的最大的区别是什么?
User password verification
IO process thread -> thread -> day5
typescript39-class类的可见修饰符
【精讲】利用原生js实现todolist
typescript40-class类的保护修饰符
Detailed explanation of MOSN reverse channel
MySQL 入门:Case 语句很好用
typescript43-类型兼容性说明
Kotlin-Flow common encapsulation class: the use of StateFlow
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
Windows 安装PostgreSQL
在树莓派上搭建属于自己的网页(2)
Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures
【Harmony OS】【ARK UI】ETS 上下文基本操作
数字孪生园区场景中的坐标知识