当前位置:网站首页>The longest substring of sword finger offer without repeated characters
The longest substring of sword finger offer without repeated characters
2022-07-28 17:05:00 【Morita Rinko】
The longest substring without repeating characters
describe :
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Answer key :
Maintain a map Used to store the traversed characters and subscripts
maxlen Used to store the maximum length value
curlen Used to store the length of the non repeating string being traversed
There are three situations :
- The current character has not appeared , That is to say map There is no information about this character in , here curlen++
- The current character minus the subscript of the last occurrence of this character is greater than curlen, It indicates that the last time this character appeared, it was in the non repeating string of the previous calculation , That is, this character is not repeated ,curlen++
- The current character has appeared in the calculation of this non repeating string , The description has been repeated ,curlen=i-index
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param s string character string
* @return int integer
*/
int lengthOfLongestSubstring(string s) {
// write code here
map<char, int> index;
int maxlen=0,curlen=0;
for(int i=0;i<s.length();i++){
if(index.find(s[i])==index.end()){
curlen++;
}else if(i-index[s[i]]>curlen){
curlen++;
}else{
curlen=i-index[s[i]];
}
if(curlen>maxlen){
maxlen=curlen;
}
// If it doesn't appear, it's to index Add elements to it , Once it appears, it is updated index value
index[s[i]]=i;
}
return maxlen;
}
};
边栏推荐
- Oracle table partition
- 【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
- Call DLL file without source code
- 【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
- 综合设计一个OPPE主页--页面的售后服务
- Easypoi --- excel file export
- 如何使用Fail2Ban保护WordPress登录页面
- Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
- College students participated in six Star Education PHP training and found jobs with salaries far higher than those of their peers
- SUSE Storage6 环境搭建详细步骤 – Win10 + VMware WorkStation
猜你喜欢

关于 CMS 垃圾回收器,你真的懂了吗?

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

概率论与数理统计第一章

Applet: get element node information

Jsonarray traversal

Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings

在AD中添加差分对及连线

Re12:读论文 Se3 Semantic Self-segmentation for Abstractive Summarization of Long Legal Documents in Low

PostgreSQL每周新闻—2022年7月20日

Interesting kotlin 0x08:what am I
随机推荐
MD5 encryption verification
SUSE Ceph 增加节点、减少节点、 删除OSD磁盘等操作 – Storage6
【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Comprehensively design an oppe homepage -- after sales service of the page
MySQL installation tutorial
【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
ticdc同步数据怎么设置只同步指定的库?
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
概率论与数理统计第一章
Rsync 服务部署与参数详解
[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)
How to use fail2ban to protect WordPress login page
[learn slam from scratch] publish the coordinate system transformation relationship to topic TF
获取时间戳的三种方法的效率比较
浏览器解码过程分析
Epoll horizontal departure, which edge triggers
Mysql与Oracle的13点区别
MySQL安装教程