当前位置:网站首页>Longest substring without duplicate characters
Longest substring without duplicate characters
2022-06-24 01:00:00 【ruochen】
Define a map Data structure storage (k,v), among key The value is the character ,value Value is character position +1, Add 1 Indicates that the character is not repeated until after the character position
- We define the starting position of non repeating substring as start, End position is end
- With end Go back and forth , Will meet with 【start,end】 If the characters in the interval are the same , In this case, the character is used as key value , Get its value value , And update the start, here 【start,end】 There are no repeating characters in the interval
- Whether updated or not start, Will update it map Data structure and results ans.
- Time complexity :O(n)
Code :
public int lengthOfLongestSubstring(String s) {int length=s.length();
int max=0;
// Store characters and
Map<Character,Integer> map =new HashMap<>();
for (int start = 0,end=0; end <length ; end++) {char element=s.charAt(end);
if (map.containsKey(element)){// To prevent continuous repetition of characters , Here is a judgment
//+1 Indicates that the next element after this element is the beginning of the non repeating string
start=Math.max(map.get(element)+1,start);
}
max=Math.max(max,end-start+1);
// Save the last location of this node ;
map.put(element,end);
}
return max;
}
边栏推荐
- An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
- C language: on the right shift of matrix
- Data management: business data cleaning and implementation scheme
- Cvpr2022 𞓜 thin domain adaptation
- Isn't this another go bug?
- 想开户炒股,通过网上进行股票开户安全吗?-
- [digital signal] spectrum refinement based on MATLAB analog window function [including Matlab source code 1906]
- 【ICPR 2021】遥感图中的密集小目标检测:Tiny Object Detection in Aerial Images
- Building a digital software factory -- panoramic interpretation of one-stop Devops platform
- LSF opens job idle information to view the CPU time/elapse time usage of the job
猜你喜欢

Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks

牛学长周年庆活动:软件大促限时抢,注册码免费送!

【小程序】实现双列商品效果

【虹科案例】3D数据如何成为可操作的信息?– 对象检测和跟踪

CVPR2022 | 可精简域适应

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

13 `bs_duixiang.tag标签`得到一个tag对象

Cvpr2022 𞓜 thin domain adaptation

使用递归形成多级目录树结构,附带可能是全网最详细注释。

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
随机推荐
【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images
Real time computing framework: Spark cluster setup and introduction case
Apple iphone14 is equipped with Beidou navigation system. What are the advantages of Beidou vs GPS?
智能制造时代下,MES管理系统需要解决哪些问题
[iccv workshop 2021] small target detection based on density map: coarse-grained density map guided object detection in aerial images
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
The concept of TP FP TN FN in machine learning
CVPR2022 | 可精简域适应
Is it safe to open an account for shares of tongdaxin?
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
C语言:递归实现N的阶乘
ARM学习(7) symbol 符号表以及调试
kubernetes之常用核心资源对象
飞桨产业级开源模型库:加速企业AI任务开发与应用
[Hongke case] how can 3D data become operable information Object detection and tracking
C language: how to solve the problem of hundreds of horses and loads
[digital signal] spectrum refinement based on MATLAB analog window function [including Matlab source code 1906]
Cvpr2022 𞓜 thin domain adaptation
Experience summary of 9 Android interviews, bytes received, Ali, advanced Android interview answer