当前位置:网站首页>[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
边栏推荐
- 详解全志V853上的ARM A7和RISC-V E907之间的通信方式
- 新版代挂网站PHP源码+去除授权/支持燃鹅代抽
- Sword finger offer 27 Image of binary tree
- PCL .vtk文件与.pcd的相互转换
- Unity与WebGL的相爱相杀
- Sword finger offer 63 Maximum profit of stock
- Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
- Unity local coordinates and world coordinates
- Failed to initialize rosdep after installing ROS
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
猜你喜欢

Understand the session, cookie and token at one time, and the interview questions are all finalized

Select sort (illustration +c code)

Unity与WebGL的相爱相杀

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

Line test - graphic reasoning - 4 - alphabetic class

How to choose the appropriate automated testing tools?

ASP. Net core introduction V

Visual studio 2019 installation

Interview questions: how to test app performance?

Loki, the "open source star picking program", realizes the efficient management of harbor logs
随机推荐
Basic knowledge of linked list
LeetCode203. Remove linked list elements
DTC社群运营怎么做?
Line test - graphic reasoning - 4 - alphabetic class
LeetCode707. Design linked list
数字化转型:五个步骤推动企业进步
ASP.NET Core入门五
Debezium series: introducing support for the final operator
行测-图形推理-1-汉字类
Leetcode19. Delete the penultimate node of the linked list [double pointer]
微生物健康網,如何恢複微生物群落
LeetCode144. Preorder traversal of binary tree
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
Variables and constants
Quick sort (diagram +c code)
Debezium series: binlogreader for source code reading
Visual design form QT designer design gui single form program
[problem] pytorch installation
Digital transformation: five steps to promote enterprise progress
Sword finger offer 55 - I. depth of binary tree