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) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int imax = 0;
int dp = 0;
int i=0;
for(; i<s.size(); ++i)
{
int j=i-1;
for(; j>=i-dp; --j)
{
if(s[j]==s[i])
{
break;
}
}
//v_DP.push_back(i-j);
dp = i-j;
if(i-j > imax)
imax = i-j;
}
return imax;
}
};
参考:上面的解法需要往前搜索,复杂度应该也是O(N),只是系数比较高;这里有个滑动窗口O(2n)复杂度的解法
http://www.2cto.com/kf/201309/245623.html
http://blog.youkuaiyun.com/likecool21/article/details/10858799