当前位置:网站首页>【Hot100】3. 无重复字符的最长子串
【Hot100】3. 无重复字符的最长子串
2022-06-28 15:51:00 【王六六的IT日常】
中等题
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
题解:
滑动窗口
定义一个 map 数据结构存储 (k, v),其中 key 值为字符,value 值为字符位置 +1,加 1 表示从字符位置后一个才开始不重复。
定义不重复子串的开始位置为 start,结束位置为 end
随着 end 不断遍历向后,会遇到与 [start, end] 区间内字符相同的情况,此时将字符作为 key 值,获取其 value 值,并更新 start,此时 [start, end] 区间内不存在重复字符
无论是否更新 start,都会更新其 map 数据结构和结果 ans。
map的value直接存字符的下标更好理解,在map.get(c)的地方再进行+1操作。
代码:
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)){
//相等字符第二次还没存进去,所以map中取到的就是之前存的一样字符的下标,start更新为下标的下一位置
start = Math.max(map.get(c) + 1 ,start);
}
res = Math.max(end - start + 1, res);
map.put(c,end);
}
return res;
}
}
边栏推荐
- Android和eclipse和MySQL上传图片并获取
- 部门新来了个字节25K出来的,让我见识到了什么是天花板
- Cross cluster deployment of helm applications using karmada
- 超自动化与网络安全的未来
- What is the maximum number of concurrent TCP connections for a server? 65535?
- wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
- wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
- Navicat 15 for MySQL
- What is the difference between treasury bonds and time deposits
- The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
猜你喜欢

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

A 24-year-old bald programmer teaches you how to continuously integrate and deliver microservice delivery. You can't learn how to cut me off
![[leetcode] 13. Roman numeral to integer](/img/3c/7c57d0c407f5302115f69f44b473c5.png)
[leetcode] 13. Roman numeral to integer

大神详解开源 BUFF 增益攻略丨直播讲座

The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative

Cross cluster deployment of helm applications using karmada

超自动化与网络安全的未来

Fleet | "backstage exploration" issue 3: status management

Visual Studio 2019软件安装包和安装教程

Privacy computing fat - offline prediction
随机推荐
Qt 界面库
一台服务器最大并发 tcp 连接数多少?65535?
Opengauss kernel: analysis of SQL parsing process
如何查询数据库中一个表中的所有数据呢?
leetcode:22. 括号生成
Realization of a springboard machine
The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
Sample explanation of batch inserting data using MySQL bulkloader
Soliciting articles and contributions - building a blog environment with a lightweight application server
IPDK — Overview
What useful supplier management systems are available
The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative
【LeetCode】13、罗马数字转整数
Tiktok actual battle ~ list of bloggers I follow, follow and check
The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
机器学习之卷积神经网络--CNN介绍
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
REDIS00_详解redis.conf配置文件
tablestore中可以使用sql查询可以查出表中所有的数据吗?