当前位置:网站首页>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;
}
};
边栏推荐
- Question making note 3 (two point search)
- Global mobile communication base station market in 2019: Ericsson, Huawei and Nokia ranked in the top three
- 传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
- parseJson
- Oracle table partition
- Record development issues
- ticdc同步数据怎么设置只同步指定的库?
- asmlinkage的理解
- Alibaba cloud - Wulin headlines - site building expert competition
- Interesting kotlin 0x0a:fun with composition
猜你喜欢

Add differential pairs and connections in Ad

Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low

小程序:scroll-view默认滑倒最下面
![[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)

Comprehensively design an oppe homepage -- page service part

【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导

Quickly master kotlin set functions

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

HTAP comes at a price

Applet: get element node information
随机推荐
Applet: get element node information
First day of QT study
PostgreSQL每周新闻—2022年7月20日
[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)
结构化设计的概要与原理--模块化
Question note 4 (the first wrong version, search the insertion position)
After paying $1.8 billion in royalties to Qualcomm, Huawei reportedly ordered 120million chips from MediaTek! Official response
[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)
parseJson
Leetcode647. Palindrome substring
3D modeling tool Archicad 26 newly released
Is smart park the trend of future development?
Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings
Rsync service deployment and parameter details
【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导
【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
技术分享 | 误删表以及表中数据,该如何恢复?
Easypoi multi sheet export by template
MySQL 5.7 and sqlyogv12 installation and use cracking and common commands
Summary of kubenertes 1.16 cluster deployment problems