当前位置:网站首页>[record of question brushing] 3 Longest substring without duplicate characters
[record of question brushing] 3 Longest substring without duplicate characters
2022-07-07 23:00:00 【InfoQ】
One 、 Title Description
- 0 <= s.length <= 5 * 104
- s By the English letters 、 Numbers 、 Symbols and spaces
Two 、 Thought analysis
- Define a record starting start And at the end of a record end For example 1 MediumabcThis window (start=0,end=3) It meets the requirements of the topic
- When the window continues to expand toabcawhen , Will not be satisfied , We can only let someone move start(start+n), Until there is no repetition , The example changes tobca.
- Keep recording the length of the window during the window scanning (end-start), Update to the largest during the move . It is the final answer we need
- At the same time, in order to quickly determine whether there are duplicate values and value positions in the acquisition window We can use a hash table to record values and positions .
3、 ... and 、 Code implementation
class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character,Integer> map = new HashMap<>();
// Records of the results That is, the maximum length of the window
int resultMax = 0;
// The first left and right boundaries of the window
int start = 0;
int end = 0;
for ( end = 0; end < s.length() ; end ++) {
if (map.containsKey(s.charAt(end))){
// When there are duplicate values in the window Move the left border of the window 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;
}
}
summary
边栏推荐
- Microbial health network, how to restore microbial communities
- 7-51 combination of two ordered linked list sequences
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- 行测-图形推理-6-相似图形类
- Leetcode19. Delete the penultimate node of the linked list [double pointer]
- Qt Graphicsview图形视图使用总结附流程图开发案例雏形
- Common verification rules of form components -2 (continuously updating ~)
- 行测-图形推理-8-图群类
- There is another problem just online... Warm
- Line test - graphic reasoning - 4 - alphabetic class
猜你喜欢
How pyGame rotates pictures
【测试面试题】页面很卡的原因分析及解决方案
How to choose the appropriate automated testing tools?
Line test - graphic reasoning - 1 - Chinese character class
知识点滴 - PCB制造工艺流程
Select sort (illustration +c code)
Nx10.0 installation tutorial
Ligne - raisonnement graphique - 4 - classe de lettres
Amesim2016 and matlab2017b joint simulation environment construction
Visual studio 2019 installation
随机推荐
Knowledge drop - PCB manufacturing process flow
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
[environment] pycharm sets the tool to convert QRC into py file
Some parameters of Haikang IPC
面试百问:如何测试App性能?
Line test - graphic reasoning - 6 - similar graphic classes
Interview questions: how to test app performance?
Sword finger offer 63 Maximum profit of stock
UWA Q & a collection
行测-图形推理-8-图群类
CTF练习
Failed to initialize rosdep after installing ROS
GBU1510-ASEMI电源专用15A整流桥GBU1510
php 获取图片信息的方法
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Redis集群安装
XMIND mind mapping software sharing
Line test - graphic reasoning - 3 - symmetric graphic class
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat