Leetcode3 ongest Substring without repeating characters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.