当前位置:网站首页>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;
}
}
边栏推荐
- 【ES实战】ES上的native realm安全方式使用
- [转]: OSGI规范 深入浅出
- [转]MySQL操作实战(一):关键字 & 函数
- SDEI初探-透过事务看本质
- Count sort
- 质量体系建设之路的分分合合
- To the distance we have been looking for -- film review of "flying house journey"
- Gbase database helps the development of digital finance in the Bay Area
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
猜你喜欢
随机推荐
每日一题-无重复字符的最长子串
用STM32点个灯
Drawing dynamic 3D circle with pure C language
YOLOv5添加注意力机制
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Haut OJ 1243: simple mathematical problems
挂起等待锁 vs 自旋锁(两者的使用场合)
浅谈JVM(面试常考)
kubeadm系列-02-kubelet的配置和启动
PMP考试敏捷占比有多少?解疑
Development error notes
Csp-j-2020-excellent split multiple solutions
[to be continued] [UE4 notes] L3 import resources and project migration
Introduction to memory layout of FVP and Juno platforms
[turn]: Apache Felix framework configuration properties
利用HashMap实现简单缓存
SAP-修改系统表数据的方法
Shell Sort
SAP method of modifying system table data
What is the agile proportion of PMP Exam? Dispel doubts
![[to be continued] [UE4 notes] L3 import resources and project migration](/img/81/6f75f8fbe60e037b45db2037d87bcf.jpg)




![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)



