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.
Code:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
bool sign[256];
int ret=0;
memset(sign,0,sizeof(sign));
for(int i=0,j=0;i<s.size();i++)
{
while(sign[s[i]]==1)
{
sign[s[j]]=0;
j++;
}
sign[s[i]]=1;
ret=max(ret,i-j+1);
}
return ret;
}
};
本文介绍了一种算法,用于解决给定字符串中找到最长无重复字符子串的问题。通过遍历字符串并使用标记数组来跟踪字符出现情况,算法能够有效计算出目标子串的长度。
947

被折叠的 条评论
为什么被折叠?



