当前位置:网站首页>[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
边栏推荐
- Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
- Basic knowledge of linked list
- Ni9185 and ni9234 hardware settings in Ni Max
- Build an "immune" barrier in the cloud to prepare your data
- Unity technical notes (II) basic functions of scriptableobject
- 数字化转型:五个步骤推动企业进步
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- Details of the open source framework of microservice architecture
- 行測-圖形推理-4-字母類
- 面试百问:如何测试App性能?
猜你喜欢
今日创见|企业促进创新的5大关键要素
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
php 获取图片信息的方法
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
What does it mean to prefix a string with F?
How pyGame rotates pictures
双非大厂测试员亲述:对测试员来说,学历重要吗?
What is fake sharing after filling the previous hole?
Redis官方ORM框架比RedisTemplate更优雅
行测-图形推理-4-字母类
随机推荐
LeetCode203. Remove linked list elements
行测-图形推理-7-相异图形类
ASP.NET Core入门五
Micro service remote debug, nocalhost + rainbow micro service development second bullet
C # realizes the communication between Modbus protocol and PLC
Common verification rules of form components -2 (continuously updating ~)
Debezium series: binlogreader for source code reading
Unity与WebGL的相爱相杀
Redis cluster installation
Force deduction - question 561 - array splitting I - step by step parsing
Debezium series: set role statement supporting mysql8
Install mxnet GPU version
消息队列与快递柜之间妙不可言的关系
Ni9185 and ni9234 hardware settings in Ni Max
Debezium系列之:mysql墓碑事件
Details of the open source framework of microservice architecture
DTC社群运营怎么做?
Form组件常用校验规则-2(持续更新中~)
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Debezium系列之:源码阅读之BinlogReader