当前位置:网站首页>每日一题-无重复字符的最长子串
每日一题-无重复字符的最长子串
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;
}
}
边栏推荐
- 【ES实战】ES上的native realm安全方式使用
- Simple HelloWorld color change
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
- [binary search] 69 Square root of X
- GBase数据库助力湾区数字金融发展
- Talking about JVM (frequent interview)
- [depth first search] 695 Maximum area of the island
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
- Haut OJ 1321: mode problem of choice sister
- Haut OJ 1357: lunch question (I) -- high precision multiplication
猜你喜欢
![[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

Research on the value of background repeat of background tiling

Web APIs DOM node

Romance of programmers on Valentine's Day

浅谈JVM(面试常考)

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

第六章 数据流建模—课后习题

Gbase database helps the development of digital finance in the Bay Area

Yolov5 ajouter un mécanisme d'attention

National teacher qualification examination in the first half of 2022
随机推荐
Reflection summary of Haut OJ freshmen on Wednesday
游戏商城毕业设计
Programmers' experience of delivering takeout
Merge sort
Haut OJ 1350: choice sends candy
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
Talking about JVM (frequent interview)
Heap sort summary
[to be continued] [depth first search] 547 Number of provinces
Es module and commonjs learning notes
Use of snippets in vscode (code template)
[to be continued] [UE4 notes] L3 import resources and project migration
[allocation problem] 455 Distribute cookies
[binary search] 69 Square root of X
A three-dimensional button
质量体系建设之路的分分合合
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
To be continued] [UE4 notes] L4 object editing
Improvement of pointnet++
软件测试 -- 0 序