当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

The present is a gift from heaven -- a film review of the journey of the soul

Binary search basis

第六章 数据流建模—课后习题
![[turn]: OSGi specification in simple terms](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[turn]: OSGi specification in simple terms

Pointnet++的改进

On-off and on-off of quality system construction
![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)
[to be continued] [UE4 notes] L1 create and configure items

SAP-修改系统表数据的方法
![[merge array] 88 merge two ordered arrays](/img/e9/a73d9f22eead8e68c1e45c27ff6e6c.jpg)
[merge array] 88 merge two ordered arrays

Count sort
随机推荐
SSH password free login settings and use scripts to SSH login and execute instructions
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
每日一题-搜索二维矩阵ps二维数组的查找
kubeadm系列-00-overview
[es practice] use the native realm security mode on es
Haut OJ 1357: lunch question (I) -- high precision multiplication
On-off and on-off of quality system construction
[binary search] 34 Find the first and last positions of elements in a sorted array
Improvement of pointnet++
Pointnet++的改进
Binary search basis
PMP candidates, please check the precautions for PMP examination in July
剑指 Offer 09. 用两个栈实现队列
挂起等待锁 vs 自旋锁(两者的使用场合)
[转]MySQL操作实战(三):表联结
[轉]: OSGI規範 深入淺出
The number of enclaves
Reverse one-way linked list of interview questions
二十六、文件系统API(设备在应用间的共享;目录和文件API)
[to be continued] [UE4 notes] L2 interface introduction