当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Count sort
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
[to be continued] [UE4 notes] L1 create and configure items
YOLOv5添加注意力機制
National teacher qualification examination in the first half of 2022
Pointnet++ learning
服务熔断 Hystrix
Reverse one-way linked list of interview questions
Palindrome (csp-s-2021-palin) solution
Introduction to tools in TF-A
随机推荐
Using HashMap to realize simple cache
Introduction to tools in TF-A
剑指 Offer 53 - II. 0~n-1中缺失的数字
Talking about JVM (frequent interview)
PMP考生,请查收7月PMP考试注意事项
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Double pointer Foundation
Haut OJ 1245: large factorial of CDs --- high precision factorial
ssh免密登录设置及使用脚本进行ssh登录并执行指令
2022上半年全国教师资格证下
每日一题-搜索二维矩阵ps二维数组的查找
sync. Interpretation of mutex source code
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Haut OJ 1241: League activities of class XXX
Chapter 6 data flow modeling - after class exercises
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[to be continued] [UE4 notes] L3 import resources and project migration
Haut OJ 1352: string of choice
Demonstration of using Solon auth authentication framework (simpler authentication framework)
[转]MySQL操作实战(三):表联结