当前位置:网站首页>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;
}
边栏推荐
- The industrial Internet era will be realized by products, technologies and models derived from the industry itself
- 现在网上开股票账户安全吗?选择国有券商,最快8分钟开户成功
- Mip-NeRF:抗混叠的多尺度神经辐射场ICCV2021
- 2021-11-21: map[i][j] = = 0, which means that (I, J) is an ocean. If you cross it, the cost will be
- [shutter] how to use shutter packages and plug-ins
- version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
- Basic usage of setfacl command
- ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
- Tiktok practice ~ one click registration and login process of mobile phone number and password (restrict mobile terminal login)
- 数字化工厂可以分为哪两类
猜你喜欢

用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧

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

What do NLP engineers do? What is the work content?

LSF opens job idle information to view the CPU time/elapse time usage of the job

Real time computing framework: Flink cluster construction and operation mechanism

【小程序】编译预览小程序时,出现-80063错误提示

ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)

Error reported using worker: uncaught domexception: failed to construct 'worker': script at***

【Flutter】如何使用Flutter包和插件
随机推荐
C语言:利用自定义函数排序
[iccv workshop 2021] small target detection based on density map: coarse-grained density map guided object detection in aerial images
How many of the 36 difficult points of activity do you know?, Android interview 2020
Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
【SPRS J P & RS 2022】小目标检测模块:A Normalized Gaussian Wasserstein Distance for Tiny Object Detection
【CVPR 2020】会议版本:A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Cvpr2022 𞓜 thin domain adaptation
Error reported using worker: uncaught domexception: failed to construct 'worker': script at***
9次Android面试经验总结,已收字节,阿里,高级android面试答案
Vs2022 save formatting plug-in
Pad User Guide
[technique of planting grass] spit blood and clean up, and take you to collect goose feathers in a fancy way! Do not spread!!!
2021-11-21: map[i][j] = = 0, which means that (I, J) is an ocean. If you cross it, the cost will be
[applet] indicator of relative path and absolute path
SQL数据库:知识点汇总,期末不挂科
JS language precision problem
Isn't this another go bug?
js输入输出语句,变量
What should I pay attention to in the interview of artificial intelligence technology?
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!