当前位置:网站首页>每日一题-无重复字符的最长子串
每日一题-无重复字符的最长子串
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;
}
}
边栏推荐
猜你喜欢
小程序直播+电商,想做新零售电商就用它吧!
对象的序列化
A new micro ORM open source framework
剑指 Offer 53 - I. 在排序数组中查找数字 I
Magnifying glass effect
游戏商城毕业设计
[转]MySQL操作实战(三):表联结
[turn to] MySQL operation practice (I): Keywords & functions
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Embedded database development programming (VI) -- C API
随机推荐
Reverse one-way linked list of interview questions
质量体系建设之路的分分合合
Programmers' experience of delivering takeout
Quick sort summary
A problem and solution of recording QT memory leakage
浅谈JVM(面试常考)
Haut OJ 1241: League activities of class XXX
[interval problem] 435 Non overlapping interval
Binary search basis
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Talking about JVM (frequent interview)
Haut OJ 1357: lunch question (I) -- high precision multiplication
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Embedded database development programming (VI) -- C API
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
PMP考生,请查收7月PMP考试注意事项
注解与反射
[binary search] 69 Square root of X
Solon Auth 认证框架使用演示(更简单的认证框架)
[turn]: Apache Felix framework configuration properties