当前位置:网站首页>【Hot100】3. Longest substring without duplicate characters
【Hot100】3. Longest substring without duplicate characters
2022-06-28 16:18:00 【Wang Liuliu's it daily】
Medium question
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Answer key :
The sliding window
Define a map Data structure storage (k, v), among key The value is the character ,value Value is character position +1, Add 1 Indicates that the character is not repeated until after the character position .
Define the start position of non repeating substring as start, End position is end
With end Go back and forth , Will meet with [start, end] If the characters in the interval are the same , In this case, the character is used as key value , Get its value value , And update the start, here [start, end] There are no repeating characters in the interval
Whether updated or not start, Will update it map Data structure and results ans.
map Of value Save the subscript of the character directly for better understanding , stay map.get(c) And then +1 operation .
Code :
class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
Map<Character,Integer> map = new HashMap<>();
for(int start = 0,end = 0;end < s.length(); end++){
char c = s.charAt(end);
if(map.containsKey(c)){
// The equivalent character has not been saved for the second time , therefore map The subscript of the same character stored before is taken from ,start Update to next position of subscript
start = Math.max(map.get(c) + 1 ,start);
}
res = Math.max(end - start + 1, res);
map.put(c,end);
}
return res;
}
}
边栏推荐
- What is the maximum number of concurrent TCP connections for a server? 65535?
- Introduction to reverse commissioning PE structure details 02/07
- 10:00面试,10:02就出来了 ,问的实在是太...
- The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- Summer Challenge ohos build custom service practice
- FFmpeg之禁止输出banner log(三十)
- Gartner发布当前至2024年的五大隐私趋势
- 如何根据多元索引查询最后一条数据,达到 sql order by desc limit 1的效果呢?
- Interviewer: how does the thread pool reuse threads? Do you know? Tell me about it
猜你喜欢

今天睡眠质量记录80分

知道这几个命令让你掌握Shell自带工具

RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52

【Hot100】1. 两数之和

FS2K人脸素描属性识别

leetcode:22. bracket-generating

General solution of island problems and DFS framework

【Golang】安装 iris 的方法

wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module

#夏日挑战赛#OHOS构建自定义服务实战
随机推荐
Today's sleep quality record is 80 points
【推荐系统】多任务学习之ESMM模型(更新ing)
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
Steps to be taken for successful migration to the cloud
岛屿类问题通用解法与DFS框架
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels
LDD 知识整理
Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger
Knowing these commands allows you to master shell's own tools
大神详解开源 BUFF 增益攻略丨直播讲座
10 years of testing experience, worthless in the face of the physiological age of 35
3. Caller 服务调用 - dapr
The future of platform as code is kubernetes extension
Traffic management and control of firewall Foundation
The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
5分钟的时间制作一个反弹球游戏
北京有哪些牛逼的中小型公司?
论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》
among us私服搭建
逆向调试入门-PE结构详解02/07