当前位置:网站首页>【刷题记录】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;
}
}

总结
边栏推荐
- Loki, the "open source star picking program", realizes the efficient management of harbor logs
- The author of LinkedList said he didn't use LinkedList himself
- “拧巴”的早教行业:万亿市场,难出巨头
- Line test - graphic reasoning -5- one stroke class
- Pyqt GUI interface and logic separation
- Leetcode19. Delete the penultimate node of the linked list [double pointer]
- Line measurement - graphic reasoning -9- line problem class
- Digital transformation: five steps to promote enterprise progress
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- 0-5vac to 4-20mA AC current isolated transmitter / conversion module
猜你喜欢

Basic knowledge of binary tree

Common verification rules of form components -2 (continuously updating ~)

Gazebo import the mapping model created by blender
Redis官方ORM框架比RedisTemplate更优雅
![LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]](/img/5e/e442c8649b9123a9d9df7c0d61a564.jpg)
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
Redis official ORM framework is more elegant than redistemplate

行測-圖形推理-4-字母類

How to choose the appropriate automated testing tools?

不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程

Unity与WebGL的相爱相杀
随机推荐
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
Redis官方ORM框架比RedisTemplate更优雅
Debezium系列之:mysql墓碑事件
XMIND mind mapping software sharing
Matplotlib quick start
【测试面试题】页面很卡的原因分析及解决方案
Digital transformation: five steps to promote enterprise progress
[environment] pycharm sets the tool to convert QRC into py file
Unity technical notes (II) basic functions of scriptableobject
Select sort (illustration +c code)
GBU1510-ASEMI电源专用15A整流桥GBU1510
Debezium系列之:源码阅读之SnapshotReader
Class implementation of linear stack and linear queue (another binary tree pointer version)
微生物健康网,如何恢复微生物群落
PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat
Microbial health network, how to restore microbial communities
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
行测-图形推理-9-线条问题类
0-5vac to 4-20mA AC current isolated transmitter / conversion module
Revit secondary development - shielding warning prompt window