Just start also a variety of optimization, afraid of n*n time complexity also pass, again want to be able to simplify, and finally found a brute force directly AC, I overestimate it ...
Test instructions is to give you a string to find the string of the eldest son, but this substring is a rule, that is, there is no repetition of the character, I started from the first character to Traverse to the last character, because the longest substring must be one of the characters to start, I counted the oldest string is OK, In fact, I have thought about it, this is actually not n*n time complexity, because the number of characters have limited, so the most is n*255 around, but this situation is still impossible, so I know, why violence can be too. ^-^
#include "stdio.h" #include "stdlib.h" #include "string" #include "string.h" using namespace Std;class solution {public: int lengthoflongestsubstring (string s) { int i,j; int len = S.size (); int str[300]; int num,max=0; for (i=0;i<len;i++) { Num = 0; memset (str,0,sizeof (str)); for (j=i;j<len;j++) { if (str[s[j]]==0) { num++; str[s[j]]=1; } else break ; } if (Num>max) { max = Num; } } printf ("%d\n", max); return max; }; int main () {solution test;string s = "eeee"; test.lengthoflongestsubstring (s); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode3 ongest Substring without repeating characters