标签:
刚开始还尼玛各种优化,怕n*n的时间复杂度还通不过,再想能不能简化一下,最后发现暴力破解直接AC,我太高估它了......
题意就是给你一个字符串,求出这个字符串的最长子串,但是这个子串是有规则的,就是不能有重复的字符,我是从第一个字符开始遍历到最后一个字符,因为最长的子串一定是以其中一个字符为开头,我逐个统计一遍最长子串就OK了,其实刚才我有想了一下,这个其实也不是n*n的时间复杂度,因为字符的个数有有限的,所以最多也就是n*255左右,但是这种情况还是不可能的,所以我知道了,为什么暴力破解可以过 . ^-^
#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;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
leetcode3 ongest Substring Without Repeating Characters
标签:
原文地址:http://blog.csdn.net/djd1234567/article/details/48147045