当前位置:网站首页>[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
边栏推荐
- Aspose. Word operation word document (I)
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- LeetCode206. Reverse linked list [double pointer and recursion]
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Line test - graphic reasoning - 3 - symmetric graphic class
- “拧巴”的早教行业:万亿市场,难出巨头
- Details of the open source framework of microservice architecture
- Leetcode94. Middle order traversal of binary trees
- 行測-圖形推理-4-字母類
- Line test - graphic reasoning - 6 - similar graphic classes
猜你喜欢
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
XMIND mind mapping software sharing
Visual design form QT designer design gui single form program
「开源摘星计划」Loki实现Harbor日志的高效管理
面试百问:如何测试App性能?
Redis cluster installation
Knowledge drop - PCB manufacturing process flow
Visual studio 2019 installation
ASP. Net core introduction V
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
随机推荐
Debezium series: introducing support for the final operator
What is ADC sampling rate (Hz) and how to calculate it
Unity technical notes (I) inspector extension
LeetCode144. Preorder traversal of binary tree
Transparent i/o model from beginning to end
消费品企业敏捷创新转型案例
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Unity technical notes (II) basic functions of scriptableobject
「开源摘星计划」Loki实现Harbor日志的高效管理
行测-图形推理-8-图群类
行测-图形推理-7-相异图形类
“拧巴”的早教行业:万亿市场,难出巨头
行测-图形推理-1-汉字类
Line test - graphic reasoning -7- different graphic classes
Build an "immune" barrier in the cloud to prepare your data
Gazebo import the mapping model created by blender
Pyqt GUI interface and logic separation
行测-图形推理-3-对称图形类
PCL . VTK files and Mutual conversion of PCD
C development -- WPF simple animation