当前位置:网站首页>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;
}
方法二:滑动窗口及优化
边栏推荐
猜你喜欢
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
接口测试框架实战(四)| 搞定 Schema 断言
Talking about GIS Data (5) - Geographic Coordinate System
Kotlin-Flow common encapsulation class: the use of StateFlow
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
【Harmony OS】【ARK UI】Date 基本操作
typescript46-函数之间的类型兼容性
Bubble sort in c language structure
随机推荐
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Coordinate knowledge in digital twin campus scenarios
Kotlin-Flow常用封装类:StateFlow的使用
How to prepare for the test interface test data
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
2022/08/02 Study Notes (day22) Multithreading
内部类、static关键字、final
VR全景展打造专属元宇宙观展空间
[Developers must see] [push kit] Collection of typical problems of push service service 2
【精讲】利用原生js实现todolist
[Harmony OS] [ArkUI] ets development graphics and animation drawing
RequestContextHolder
js garbage collection mechanism
typescript46-函数之间的类型兼容性
建立树形结构
Interface Test Framework Practice (4) | Get Schema Assertion
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
索引创建、删除与使用
Alienware上线首个数字时装AR试穿体验