leetcode3 ongest Substring Without Repeating Characters

刚开始还尼玛各种优化,怕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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 03:48:59

leetcode3 ongest Substring Without Repeating Characters的相关文章

LeetCode3 Longest Substring Without Repeating Characters

题目: 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 s

LeetCode3:Longest Substring Without Repeating Characters

题目: 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 s

[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

leetcode4 ---Longest Substring Without Repeating Characters

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 subst

LeetCode---Longest Substring Without Repeating Characters

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 subst

LeetCode: Longest Substring Without Repeating Characters 题解

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 subst

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode 3_Longest Substring Without Repeating Characters

题目描述: 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