您的位置:首页 > 其它

No3.Longest Substring Without Repeating Characters

2015-07-26 22:05 369 查看
/*
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Longest Substring Wi