当前位置:网站首页>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;
}
}
边栏推荐
- Chapter 6 data flow modeling - after class exercises
- C language Essay 1
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
- The number of enclaves
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- [转]: OSGI规范 深入浅出
- Control Unit 控制部件
- High precision subtraction
- 用STM32点个灯
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
猜你喜欢
随机推荐
[binary search] 34 Find the first and last positions of elements in a sorted array
Heap sort summary
Fragment addition failed error lookup
A problem and solution of recording QT memory leakage
YOLOv5-Shufflenetv2
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
剑指 Offer 53 - I. 在排序数组中查找数字 I
Haut OJ 1243: simple mathematical problems
【ES实战】ES上的native realm安全方式使用
object serialization
Reflection summary of Haut OJ freshmen on Wednesday
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Es module and commonjs learning notes -- ESM and CJS used in nodejs
卷积神经网络——卷积层
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Software test -- 0 sequence
SDEI初探-透过事务看本质
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[转]: OSGI规范 深入浅出