最長無重複子串

2021-08-13 06:59:17 字數 602 閱讀 3256

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 the length of 3. note that the answer must be a substring, 「pwke」 is a subsequence and not a substring.

解題思路:

從首字母開始遍歷計算以每乙個字母結尾的最長無重複子串的長度。

int lengthoflongestsubstring(string s) 

return maxlen;

}

申請乙個足夠大的空間dict來儲存已遍歷字元的序號,如有重複字元,則儲存最大序號。假設字串"webkecba",dict[s[i]] > start(start為-1或者前一對重複陣列的第乙個字元序號),表示在前一對重複字元的第乙個字元之後有與s[i]重複的字元。該程式的時間複雜度為o(n)。

最長無重複子串

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

最長無重複子串

最長無重複子串 兩種思路 一種是暴力解法 一種是動態規劃 package merchant public class maxnorepeat int record 0,maxlen 1,index 0 for int i 1 ilength i else len if maxlenreturn s....

leetcode 求最長無重複子串

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