当前位置:网站首页>每日一题-无重复字符的最长子串
每日一题-无重复字符的最长子串
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;
}
}
边栏推荐
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- Embedded database development programming (VI) -- C API
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- room数据库的使用
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- [allocation problem] 455 Distribute cookies
- [轉]: OSGI規範 深入淺出
- YOLOv5-Shufflenetv2
- Merge sort
- GBase数据库助力湾区数字金融发展
猜你喜欢

挂起等待锁 vs 自旋锁(两者的使用场合)
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research

Using HashMap to realize simple cache

Double pointer Foundation

object serialization

质量体系建设之路的分分合合

Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!

Magnifying glass effect

Pointnet++的改进

Service fusing hystrix
随机推荐
Es module and commonjs learning notes
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
[turn to] MySQL operation practice (III): table connection
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Haut OJ 1245: large factorial of CDs --- high precision factorial
一个新的微型ORM开源框架
The next key of win generates the timestamp file of the current day
Programmers' experience of delivering takeout
Gbase database helps the development of digital finance in the Bay Area
二十六、文件系统API(设备在应用间的共享;目录和文件API)
[turn]: OSGi specification in simple terms
Binary search basis
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Download xftp7 and xshell7 (official website)
剑指 Offer 04. 二维数组中的查找
C language Essay 1
Using HashMap to realize simple cache
Quick sort summary
On-off and on-off of quality system construction