当前位置:网站首页>Daily question - longest substring without repeated characters
Daily question - longest substring without repeated characters
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Problem analysis ( This question uses a sliding window +HashMap)
- So let's set up a map aggregate ( Record the storage location of different strings ) And a temporary variable to store the longest character length
- Use double pointer end, start; Traversal string , First turn on the end Take out the characters of the pointer
- If map Set has the same characters , Change the starting position
- Otherwise, update Res Value , stay map Sets store different characters and their positions .
Code instance
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);// Why? end+1 You have to be careful ,start Guarantee start The starting position should be the next digit of the repeated string .
}
return res;
}
}
边栏推荐
- kubeadm系列-00-overview
- Talking about JVM (frequent interview)
- [turn]: Apache Felix framework configuration properties
- Csp-j-2020-excellent split multiple solutions
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- Time complexity and space complexity
- YOLOv5-Shufflenetv2
- A problem and solution of recording QT memory leakage
- 剑指 Offer 05. 替换空格
- 剑指 Offer 35.复杂链表的复制
猜你喜欢
随机推荐
每日一题-搜索二维矩阵ps二维数组的查找
[binary search] 69 Square root of X
YOLOv5-Shufflenetv2
Use of room database
To be continued] [UE4 notes] L4 object editing
Pointnet++的改进
Haut OJ 1218: maximum continuous sub segment sum
剑指 Offer 04. 二维数组中的查找
卷积神经网络——卷积层
Haut OJ 1245: large factorial of CDs --- high precision factorial
读者写者模型
Summary of Haut OJ 2021 freshman week
远程升级怕截胡?详解FOTA安全升级
剑指 Offer 05. 替换空格
[转]:Apache Felix Framework配置属性
[turn]: Apache Felix framework configuration properties
Detailed explanation of expression (csp-j 2021 expr) topic
剑指 Offer 09. 用两个栈实现队列
Drawing dynamic 3D circle with pure C language
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export









