当前位置:网站首页>[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
边栏推荐
- Revit secondary development - intercept project error / warning pop-up
- Take full control! Create a "leading cockpit" for smart city construction
- Ren Qian code compilation error modification
- The PHP source code of the new website + remove authorization / support burning goose instead of pumping
- Ligne - raisonnement graphique - 4 - classe de lettres
- 全面掌控!打造智慧城市建设的“领导驾驶舱”
- Line test - graphic reasoning -5- one stroke class
- 行测-图形推理-4-字母类
- Build an "immune" barrier in the cloud to prepare your data
- Failed to initialize rosdep after installing ROS
猜你喜欢

C # realizes the communication between Modbus protocol and PLC

行测-图形推理-8-图群类

What does the model number of asemi rectifier bridge kbpc1510 represent

今日创见|企业促进创新的5大关键要素

There is another problem just online... Warm

The PHP source code of the new website + remove authorization / support burning goose instead of pumping

UWA Q & a collection

How pyGame rotates pictures

「开源摘星计划」Loki实现Harbor日志的高效管理

Unity与WebGL的相爱相杀
随机推荐
IP network active evaluation system -- x-vision
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
CTF练习
Debezium系列之:支持 mysql8 的 set role 语句
Leetcode206. Reverse linked list
Gazebo import the mapping model created by blender
ASP. Net core introduction V
行测-图形推理-4-字母类
Sword finger offer 28 Symmetric binary tree
小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
UWA Q & a collection
Leetcode94. Middle order traversal of binary trees
Basic knowledge of linked list
Qt Graphicsview图形视图使用总结附流程图开发案例雏形
ASEMI整流桥KBPC1510的型号数字代表什么
Debezium series: MySQL tombstone event
Pyqt GUI interface and logic separation
Quick sort (diagram +c code)
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
PCL .vtk文件与.pcd的相互转换