当前位置:网站首页>每日一题-无重复字符的最长子串
每日一题-无重复字符的最长子串
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
问题解析(本题使用滑动窗口+HashMap)
- 首先建立一个map集合(记录不同字符串的存放位置)以及一个临时变量存储最长字符长度
- 使用双指针 end, start;遍历字符串,首先把end指针的字符取出来
- 如果map集合有相同的字符,更改起始位置
- 否则就更新Res的值,在map集合存储不相同字符以及字符的位置。
代码实例
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
int res = 0;
Map<Character,Integer> map = new HashMap<>();
for(int end = 0, start = 0; end < n; end++){
char c = s.charAt(end);
if(map.containsKey(c)){
start = Math.max(map.get(c),start);
}
res = Math.max(res,end - start + 1);
map.put(s.charAt(end),end + 1);//为什么end+1需要注意下,start保证start起始位置要在重复字符串的下一位。
}
return res;
}
}
边栏推荐
- [speed pointer] 142 circular linked list II
- 剑指 Offer 04. 二维数组中的查找
- Service fusing hystrix
- Embedded database development programming (V) -- DQL
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- Solon 框架如何方便获取每个请求的响应时间?
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
- The present is a gift from heaven -- a film review of the journey of the soul
- Haut OJ 1221: a tired day
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
猜你喜欢

小程序直播+电商,想做新零售电商就用它吧!

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

Simple modal box

TF-A中的工具介绍
![[转]MySQL操作实战(三):表联结](/img/70/20bf9b379ce58761bae9955982a158.png)
[转]MySQL操作实战(三):表联结

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

Embedded database development programming (VI) -- C API

2022年上半年国家教师资格证考试

SAP-修改系统表数据的方法

sync. Interpretation of mutex source code
随机推荐
Shell Sort
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
剑指 Offer 04. 二维数组中的查找
Add level control and logger level control of Solon logging plug-in
[allocation problem] 455 Distribute cookies
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
GBase数据库助力湾区数字金融发展
SSH password free login settings and use scripts to SSH login and execute instructions
Bubble sort summary
ssh免密登录设置及使用脚本进行ssh登录并执行指令
服务熔断 Hystrix
挂起等待锁 vs 自旋锁(两者的使用场合)
SDEI初探-透过事务看本质
Improvement of pointnet++
[turn to] MySQL operation practice (III): table connection
[turn]: OSGi specification in simple terms
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Page countdown
[allocation problem] 135 Distribute candy
[转]:Apache Felix Framework配置属性