当前位置:网站首页>【刷题记录】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实现Harbor日志的高效管理
- Aspose. Words merge cells
- Why is network i/o blocked?
- Line measurement - graphic reasoning -9- line problem class
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- Unity与WebGL的相爱相杀
- Select sort (illustration +c code)
- Ligne - raisonnement graphique - 4 - classe de lettres
- Revit secondary development - link file collision detection
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
猜你喜欢

微生物健康網,如何恢複微生物群落

Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb

Line test - graphic reasoning - 2 - black and white lattice class

肠道里的微生物和皮肤上的一样吗?

What is fake sharing after filling the previous hole?

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

How to judge whether the input content is "number"
![[environment] pycharm sets the tool to convert QRC into py file](/img/4f/d1c811dea23e3695a8b6d04b938bb5.jpg)
[environment] pycharm sets the tool to convert QRC into py file

Early childhood education industry of "screwing bar": trillion market, difficult to be a giant

行测-图形推理-4-字母类
随机推荐
Revit secondary development - operation family documents
行测-图形推理-7-相异图形类
Unity technical notes (I) inspector extension
PHP method of obtaining image information
Ligne - raisonnement graphique - 4 - classe de lettres
How pyGame rotates pictures
ASP.NET Core入门五
Ren Qian code compilation error modification
ASEMI整流桥KBPC1510的型号数字代表什么
一次搞明白 Session、Cookie、Token,面试问题全稿定
行测-图形推理-4-字母类
php 获取图片信息的方法
行测-图形推理-6-相似图形类
IP network active evaluation system -- x-vision
Ni9185 and ni9234 hardware settings in Ni Max
Microbial health network, how to restore microbial communities
Leetcode interview question 02.07 Linked list intersection [double pointer]
Debezium系列之:源码阅读之BinlogReader
行測-圖形推理-4-字母類
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853