/*
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 charArray[256]={0};
int len=0,maxLen=0,pre =0;
for (int i=0;i < s.length();i++)
{
if (charArray[s[i]]<pre)
{
charArray[s[i]] = i+1;
len++;
}
else
{
pre = charArray[s[i]];
len = i +1 - charArray[s[i]] ;
charArray[s[i]] = i+1;
}
maxLen = (maxLen<len)?len:maxLen;
}
return maxLen;
}
};
No3.Longest Substring Without Repeating Characters
最新推荐文章于 2024-01-06 13:50:14 发布