## [&] () {}

Life is short , Import your DREAM.独学无友，则孤陋而难成；久处一方，则习染而不自觉。...

# 题目：

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 index = 0;
int max = 0;
int len = s.length();
if( len == 0)
return 0;
if (len ==1)
return 1;
for (int i =1; i< len; i++)
{
for(int j=i-1; j>=index; j--)
{
if(s[i] == s[j])
{
index = j+1;
break;
}
else
{
if(max < i-j+1)
max = i - j +1;
}
}
}
return max;
}
};

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int start = 0; // current start point of substring without dup
int maxlen = 0; // max length of substring found
int table[256]; // hash table for index of each char appeared
for (int i = 0;i < 256;i++) table[i] = -1; // if char not present, index is -1
int len = s.length();
for (int i = 0;i < len;i++) {
if (table[s[i]] != -1) {
while (start <= table[s[i]]) table[s[start++]] = -1;
}
if (i - start + 1 > maxlen) maxlen = i - start + 1;
table[s[i]] = i;
}
return maxlen;
}
};

11-23 1.7万

01-19 176

04-05 4462

02-18 4841

07-15 942

04-01 6579

12-04 1255

05-26 2473

08-18 815