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

总结
边栏推荐
- Pyqt GUI interface and logic separation
- 行测-图形推理-7-相异图形类
- Debezium系列之:mysql墓碑事件
- Redis集群安装
- Debezium series: set role statement supporting mysql8
- Aspose. Word operation word document (I)
- Ni9185 and ni9234 hardware settings in Ni Max
- Class implementation of linear stack and linear queue (another binary tree pointer version)
- Leetcode19. Delete the penultimate node of the linked list [double pointer]
- GBU1510-ASEMI电源专用15A整流桥GBU1510
猜你喜欢
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Robot autonomous exploration series papers environment code
Redis集群安装
ASP.NET Core入门五
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
Line test - graphic reasoning -7- different graphic classes
行测-图形推理-7-相异图形类
LeetCode144. Preorder traversal of binary tree
Common verification rules of form components -2 (continuously updating ~)
Line test - graphic reasoning - 4 - alphabetic class
随机推荐
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Debezium series: set role statement supporting mysql8
Some parameters of Haikang IPC
Leetcode19. Delete the penultimate node of the linked list [double pointer]
Debezium系列之:mysql墓碑事件
Redis official ORM framework is more elegant than redistemplate
Gazebo import the mapping model created by blender
ASEMI整流桥KBPC1510的型号数字代表什么
How to judge whether the input content is "number"
C development -- WPF simple animation
Line test graph reasoning graph group class
Unity 动态合并网格纹理
Line measurement - graphic reasoning -9- line problem class
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
Unity FAQ (I) lack of references
Two minutes, talk about some wrong understandings of MySQL index
Force deduction - question 561 - array splitting I - step by step parsing
微生物健康网,如何恢复微生物群落
消费品企业敏捷创新转型案例
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853