当前位置:网站首页>Sword finger offer II 016 Longest substring without duplicate characters
Sword finger offer II 016 Longest substring without duplicate characters
2022-06-10 00:50:00 【Small white yards fly up】
Summary
Substring problem , The double pointer must be right !
subject
Given a string s , Please find out that there are no duplicate characters in it Longest continuous substring The length of .

Ideas
Questions involving continuous characters or arrays , The first reaction is double pointer .
The right pointer moves to the left , We save every character Between the current left and right pointers First occurrence of subscript . When repeated characters appear , Determine whether the maximum length is between the left and right pointers and save . Then judge the subscript of the character for the first time , Then the left pointer jumps directly to the left of the character .
Pay attention to various boundary problems , Be careful .
solution
Code
public int lengthOfLongestSubstring(String s) {
int left = 0;
Map<Character, Integer> map = new HashMap<>();
int maxLength = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// Encountered a duplicate character in the left and right pointers , Then judge the longest length , And move the left pointer to the new position
if (map.containsKey(c) && map.get(c) >= left) {
maxLength = Math.max(maxLength, i - left);
left = map.get(c) + 1;
}
// The boundary processing of the right pointer moving to the last bit
if (i == s.length() - 1) {
maxLength = Math.max(maxLength, i - left + 1);
}
// No other issues need to be considered , Directly update the current string to the current subscript position
map.put(c, i);
}
return maxLength;
}
边栏推荐
- run npm fund for details
- Where is the precise position of PS ruler adjustment
- [typecho] how to query multiple tables
- Manjaro kconsole turns on translucency
- BGP protocol experiment
- 线性规划和对偶规划学习总结
- 力扣 自除数 C语言 题解
- Apply the latest ad and Txk patches
- Binary search (half search) summary
- [docker]mysql scheduled backup
猜你喜欢

重发布实验

Hcip day 1 Summary

第6章域控制器安全

How WPS merges cells with different sizes
力扣 两数之和 C语言 题解

Code case - web version confession wall and file upload
Blue Bridge Cup · winter vacation hundred schools' real topic league tournament (Phase V) real topic exercise of cargo placement for graduate students and university group A

ps标尺调精确位置在哪

Gartner global IAAs report in 2021: AWS cake is nibbled, and Chinese cloud manufacturers are steadily attacking

Win10 how to delete an account
随机推荐
ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
内网渗透第4章
RHCSA第一天
Aquanee will land in gate and bitmart in the near future, providing a good opportunity for low-level layout
fer2013的csv文件转换为ImageNet格式(以图片格式保存在各自类别的文件夹中)
BGP experiment
Bubble sorting and optimization clear and intuitive C language
win10怎么删除账户
IDC authority predicts that China's manufacturing industry will soon take advantage of the cloud
Score of sub series of previous test questions and [11th] [provincial competition] [group B]
BGP实验
How to keep the contents of WPS merged cells in one cell
蓝桥杯·寒假百校真题大联赛(第五期)研究生&大学A组 《货物摆放》真题练习
OSPF first experiment
应用最新的AD和TXK补丁
AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机
wps文本框如何设置透明
Pycharm 2022 permanently activated version download, valid for personal test
Apply the latest ad and Txk patches
Rhcsa day 3