当前位置:网站首页>【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;
}
}
边栏推荐
- Super automation and the future of network security
- PostgreSQL异常处理
- 北京有哪些牛逼的中小型公司?
- Briefly introduce the conversion between tensorflow and pytorch (mainly tensorflow to pytorch)
- #夏日挑战赛#OHOS构建自定义服务实战
- QQ appears large-scale number theft, why is this? Is there no solution?
- A new 25K byte from the Department showed me what the ceiling is
- 3. caller service call - dapr
- AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
- CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
猜你喜欢

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
![[MySQL] official website document learning query statement SQL precautions](/img/aa/bf27b609e2fda1edaa46f134fc5626.png)
[MySQL] official website document learning query statement SQL precautions
![[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels](/img/35/63c9793ec7bc1c90c759504e84dc96.png)
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels

Today's sleep quality record is 80 points

A new 25K byte from the Department showed me what the ceiling is

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

Azure Kinect微软摄像头Unity开发小结

Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52

among us私服搭建

字节跳动数据平台技术揭秘:基于ClickHouse的复杂查询实现与优化
随机推荐
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
【Redis】2021/01/31 Redis的简单归纳 No.01
QQ出现大规模盗号,为什么会这样?就没有解决方法了吗?
【Laravel】关于Laravel8的composer安装
元宇宙中能接吻了!CMU推出VR头显外挂,复刻唇部逼真触觉
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
Android和eclipse和MySQL上传图片并获取
今天睡眠质量记录80分
Mysql自連接查詢「建議收藏」
早晨有些犹豫
Sample explanation of batch inserting data using MySQL bulkloader
媒体数据处理V2版本(VPC)图像缩放内容解析
简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
【MySQL】表连接为什么比子查询快
A new 25K byte from the Department showed me what the ceiling is
QT create 5.0.3 configuring qt4.8.7
Cross cluster deployment of helm applications using karmada
QT interface library
【Golang】安装 iris 的方法
Traffic management and control of firewall Foundation