当前位置:网站首页>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;
}
}
边栏推荐
- Using HashMap to realize simple cache
- Haut OJ 2021 freshmen week II reflection summary
- [es practice] use the native realm security mode on es
- Quick sort summary
- [to be continued] [UE4 notes] L1 create and configure items
- 2022年上半年国家教师资格证考试
- [turn to] MySQL operation practice (III): table connection
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- Maximum number of "balloons"
猜你喜欢
随机推荐
Shell Sort
[allocation problem] 135 Distribute candy
Palindrome (csp-s-2021-palin) solution
To be continued] [UE4 notes] L4 object editing
A preliminary study of sdei - see the essence through transactions
Count sort
Haut OJ 1241: League activities of class XXX
第六章 数据流建模—课后习题
支持多模多态 GBase 8c数据库持续创新重磅升级
Pointnet++ learning
剑指 Offer 09. 用两个栈实现队列
Haut OJ 1357: lunch question (I) -- high precision multiplication
Csp-j-2020-excellent split multiple solutions
What is the agile proportion of PMP Exam? Dispel doubts
[depth first search] 695 Maximum area of the island
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
读者写者模型
Solution to the palindrome string (Luogu p5041 haoi2009)
Gbase database helps the development of digital finance in the Bay Area
Es module and commonjs learning notes