最長無重複子串

2021-10-03 20:45:51 字數 1150 閱讀 6010

最長無重複子串

兩種思路:一種是暴力解法

一種是動態規劃

package merchant;

public

class

maxnorepeat

int record=

0,maxlen=

1,index=0;

for(

int i=

1;ilength()

;i++

)else len++;if

(maxlenreturn s.

substring

(index,index+maxlen);}

//滑動視窗法

// public static string maxsub2(string s)

// //

// }

//動態規劃,加入當前位置的最大的子字串是str[i],則下乙個位置的最大無重複子字串有兩種情況,1,當前字元未

//在str[i]中,則str[i+1] = str[i]+char[i+1];

在str[i]中,則str[i+1]=在str[i]中重複元素的所在位置的下乙個元素直到str[i]末尾+char[i+1]

//例如str[i]=「abcd」,char[i+1]="a",則str[i+1] = "bcd"+"a";

public

static string maxsub2

(string s)

char

c = s.

tochararray()

;int len = c.length;

string[

] str =

newstring

[len]

; str[0]

= c[0]

+"";for

(int i=

1;i)else

}int maxlen =0;

int index =0;

for(

int i=

0;ireturn str[index];}

public

static

void

main

(string[

] args)

}

最長無重複子串

examples given abcabcbb the answer is abc which the length is 3.given bbbbb the answer is b with the length of 1.given pwwkew the answer is wke with t...

最長無重複子串

給定乙個字串,請找出其中無重複字元的最長子字串。例如,在 abcabcbb 中,最長無重複子串為 abc 其長度為3。在 pwwkew 最長無重複子串為 wke 其長度為3。注意是是子串,pwke 是子串行而不是子串。leetcode 首先試了試暴力搜尋,遍歷一次字串,遍歷過程中,對每個字元都有乙個...

leetcode 求最長無重複子串

1.求最長無重複子串 給定乙個字串,找出不含有重複字元的最長子串的長度。示例 給定 abcabcbb 沒有重複字元的最長子串是 abc 那麼長度就是3。給定 bbbbb 最長的子串就是 b 長度是1。給定 pwwkew 最長子串是 wke 長度是3。請注意答案必須是乙個子串,pwke 是 子串行 而...