当前位置:网站首页>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;
}
};
边栏推荐
- kubenertes 1.16集群部署问题总结
- Detailed steps for setting up SUSE storage6 environment – win10 + VMware Workstation
- ticdc同步数据怎么设置只同步指定的库?
- ERROR: transport library not found: dt_ socket
- 传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
- [deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)
- Exercise note 5 (square of ordered array)
- Do you really understand CMS garbage collector?
- Deep understanding of deepsea and salt deployment tools – storage6
- SUSE Storage6 环境搭建详细步骤 – Win10 + VMware WorkStation
猜你喜欢

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

Do you really understand CMS garbage collector?

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

HTAP comes at a price

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)

Interesting kotlin 0x08:what am I

Easypoi --- excel file export

Interesting kotlin 0x07:composition

【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
随机推荐
MySQL installation tutorial
TCP handshake, waving, time wait connection reset and other records
Technology sharing | how to recover the erroneously deleted table and the data in the table?
Detailed steps for setting up SUSE storage6 environment – win10 + VMware Workstation
浏览器解码过程分析
Leetcode9. Palindromes
记录开发问题
Oracle system composition
Applet: get element node information
Egg (19): use egg redis performance optimization to cache data and improve response efficiency
Simple addition, deletion, modification and query of commodity information
Oracle table partition
SUSE Ceph 快速部署 – Storage6
[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)
Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints
Reduce cycle complexity
Go language slow entry - process control statement
做题笔记4(第一个错误的版本,搜索插入位置)
深入理解 DeepSea 和 Salt 部署工具 – Storage6
概率论与数理统计第一章