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

方法二:滑动窗口及优化


边栏推荐
- Interface test practice | Detailed explanation of the difference between GET / POST requests
- typescript49-交叉类型
- 【Harmony OS】【FAQ】Hongmeng Questions Collection 1
- VR全景展打造专属元宇宙观展空间
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- presto安装部署教程
- 在竞争白热化的电商行业,链动2+1为什么还有企业在用
- 建立树形结构
- 【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
- Get the Ip tool class
猜你喜欢

软件开发的最大的区别是什么?

Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

4.深度学习的几何解释与梯度的优化

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

2022/08/02 学习笔记 (day22) 多线程

Shell之条件语句

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

私域流量引流方法?分享购火爆的商业模式,你值得拥有

常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
随机推荐
redis键值出现 xacxedx00x05tx00&的解决方法
Interface testing framework of actual combat (2) | interface request assertion
数字孪生园区场景中的坐标知识
Redis缓存雪崩、缓存穿透、缓存击穿
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
js garbage collection mechanism
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
数据库基本概述与SQL概述
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
Interface testing framework combat (3) | JSON request and response assertion
MOSN 反向通道详解
【开发者必看】【push kit】推送服务服务典型问题合集2
Flink状态
MySQL 出现 The table is full 的解决方法
[Harmony OS] [ARK UI] ETS context basic operations
数字化时代,企业如何建立自身的云平台与商业模式的选择?
Interface test framework combat (1) | Requests and interface request construction
Shell conditional statement judgment
接口测试实战| GET/POST 请求区别详解
c语言结构体中的冒泡排序