leetcode3 longest Substring Without Repeating Characters,leetcode3repeating

來源:互聯網
上載者:User

leetcode3 longest Substring Without Repeating Characters,leetcode3repeating

剛開始還尼瑪各種最佳化,怕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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.