首页 > 其他分享 >LeetCode OJ Longest Substring Without Repeating Characters 不重复的最长字串 滑动窗口

LeetCode OJ Longest Substring Without Repeating Characters 不重复的最长字串 滑动窗口

时间:2023-02-20 16:36:53浏览次数:44  
标签:substring OJ int max Substring length Without longest id


Longest Substring Without Repeating Characters


Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

class Solution {
public:
int lengthOfLongestSubstring(string s) {

int has[256];
memset(has,-1,sizeof(has));
int i,id=-1,max=0;
for(i=0;i<s.size();i++){

if(has[s[i]]>id)
{
id=has[s[i]];
}
if(i-id>max)
{
max=i-id;
}
has[s[i]]=i;
}
return max;
}
};

标签:substring,OJ,int,max,Substring,length,Without,longest,id
From: https://blog.51cto.com/u_1382267/6068688

相关文章