当前位置:网站首页>【刷题记录】3. 无重复字符的最长子串
【刷题记录】3. 无重复字符的最长子串
2022-07-07 21:49:00 【InfoQ】
一、题目描述
- 0 <= s.length <= 5 * 104
- s 由英文字母、数字、符号和空格组成
二、思路分析
- 定义一个记录起始的start 和一个记录末尾的 end 如例1 中的abc这个窗口(start=0,end=3)是满足题目要求的
- 当窗口继续扩大为abca时,就会不满足,我们只有让有移动start(start+n),知道没有重复的为止,例子移动后变为bca。
- 在窗口扫描的过程中一直记录窗口的长度(end-start),更新为移动过程中最大的。既为我们需要的最终答案
- 同时为了更快判断的获取窗口内是否存在重复的值和值的位置 我们可以用一个哈希表来记录值和位置。
三、代码实现
class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character,Integer> map = new HashMap<>();
//记录结果 即窗口的最大长度
int resultMax = 0;
//窗口最开始的左边界和右边界
int start = 0;
int end = 0;
for ( end = 0; end < s.length() ; end ++) {
if (map.containsKey(s.charAt(end))){
//窗口内存在重复的值时 移动窗口的左边界start
start = Math.max(start,map.get(s.charAt(end)) + 1);
}
map.put(s.charAt(end),end);
resultMax = Math.max(resultMax,end - start + 1 );
}
return resultMax;
}
}

总结
边栏推荐
- Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
- Redis cluster installation
- The PHP source code of the new website + remove authorization / support burning goose instead of pumping
- This time, let's clear up: synchronous, asynchronous, blocking, non blocking
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
- 行测-图形推理-4-字母类
- Leetcode1984. Minimum difference in student scores
- 微生物健康網,如何恢複微生物群落
- Unity development --- the mouse controls the camera to move, rotate and zoom
- Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
猜你喜欢

ASP.NET Core入门五

Form组件常用校验规则-2(持续更新中~)

Leetcode206. Reverse linked list

ASEMI整流桥KBPC1510的型号数字代表什么
Redis官方ORM框架比RedisTemplate更优雅

Matplotlib quick start

Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment

Transparent i/o model from beginning to end

数字藏品加速出圈,MarsNFT助力多元化文旅经济!
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
随机推荐
This time, let's clear up: synchronous, asynchronous, blocking, non blocking
QT graphicsview graphical view usage summary with flow chart development case prototype
Redis集群安装
What is ADC sampling rate (Hz) and how to calculate it
Revit secondary development - operation family documents
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
C development -- WPF simple animation
Personal statement of testers from Shuangfei large factory: is education important for testers?
UWA Q & a collection
php 获取图片信息的方法
Debezium series: binlogreader for source code reading
Two minutes, talk about some wrong understandings of MySQL index
Force deduction - question 561 - array splitting I - step by step parsing
Debezium系列之: 支持在 KILL 命令中使用变量
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
XMIND mind mapping software sharing
Debezium series: source code reading snapshot reader
Failed to initialize rosdep after installing ROS
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Redis cluster installation