Create 3.cpp

This commit is contained in:
Kirigaya Kazuto 2018-06-20 22:57:09 +08:00 committed by GitHub
parent f13c712efa
commit efb59f18ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

38
LeetCode-CN/3.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <cstdlib>
#include <cstring>
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int sz = s.size();
int bin[256];
int maxlen = 0;
int clen = 0;
int L = 0;
while (L < sz)
{
memset(bin, 0, sizeof(int) * 256);
int i;
for (i = L; i < sz; i++)
{
if (bin[s[i]])
{
maxlen = clen > maxlen ? clen : maxlen;
clen = 0;
L++;
break;
}
else
{
bin[s[i]] = 1;
clen++;
}
}
if (i == sz)
{
maxlen = clen > maxlen ? clen : maxlen;
break;
}
}
return maxlen;
}
};