成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Longest Substring Without Repeating Cha

CoderStudy / 2355人閱讀

摘要:建立數(shù)組,存儲個字符最近一次出現(xiàn)的位置。首次出現(xiàn)某字符時,其位置標記為,并用無重復字符計數(shù)器記錄無重復字符的長度,再在更新其最大值。循環(huán)完整個字符串后,返回最大值。

Problem

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.

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.

Note

建立int數(shù)組ch[],存儲256個字符最近一次出現(xiàn)的位置。首次出現(xiàn)某字符a時,其位置標記為ch[a],并用無重復字符計數(shù)器count記錄無重復字符的長度,再在max更新其最大值。

當出現(xiàn)重復字符a時,從無重復字符計數(shù)器count減去兩個重復字符之間的長度(ch[a]-start),并更新start為最后一個(任意)重復字符上一次出現(xiàn)的位置,然后更新重復字符的位置ch[a]。

循環(huán)完整個字符串后,返回最大值max

舉例說明:

"pwwkewk"
start = 0, count = 0, max = 0

i = 0:

a = "p"
ch["p"] = 0 + 1 = 1
count = 1
max = 1

i = 1:

a = "w"
ch["w"] = i + 1 = 2
count = 2
max = 2

i = 2:

a = "w"
ch["w"] = 2 > start = 0
    count = count - (ch["w"] - start) = 2 - (2 - 0) = 0
    start = 2
ch["w"] = 2 + 1 = 3
count = 1
max = 2

i = 3:

a = "k"
ch["k"] = 3 + 1 = 4
count = 2
max = 2

i = 4:

a = "e"
ch["e"] = 5
count = 3
max = 3

i = 5:

a = "w"
ch["w"] = 3 > start = 2
    count = count - (ch["w"] - start) = 3 - (3 - 2) = 2
    start = 3
ch["w"] = 6
count = 2 + 1 = 3
max = 3

i = 6:

a = "k"
ch["k"] = 4
start = 3
    count = count - (ch["k"] - start) = 3 - (4 - 3) = 2
    start = 4
ch["k"] = 7
count = 3
max = 3
Solution
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] ch = new int[256];
        int max = 0, count = 0, start = 0;
        for (int i = 0; i < s.length(); i++) {
            char a = s.charAt(i);
            if (ch[a] > start) {
                count -= ch[a] - start;
                start = ch[a];
            }
            ch[a] = i+1;
            max = Math.max(max, ++count);
        }
        return max;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/66027.html

相關文章

  • [Leetcode] Longest Substring Without Repeating Cha

    摘要:哈希表是最自然的想法。在遍歷字符串時,我們先根據(jù)哈希表找出該字符上次出現(xiàn)的位置,如果大于等于子字符串首,便更新子字符串首。結束后,將該字符新的位置放入哈希表中。 Longest Substring Without Repeating Characters 最新更新解法:https://yanjia.me/zh/2018/12/... Given a string, find the ...

    FleyX 評論0 收藏0
  • [LeetCode] Longest Substring Without Repeating Cha

    Problem 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...

    graf 評論0 收藏0
  • leetcode 3 Longest Substring Without Repeating Cha

    摘要:題目詳情題目要求輸入一個字符串,我們要找出其中不含重復字符的最長子字符串,返回這個最長子字符串的長度。對于字符串中的每一個字符,先判斷中是否已經存在這個字符,如果不存在,直接將添加到,如果已存在,則新的字符串就從不含前一個字符的地方開始。 題目詳情 Given a string, find the length of the longest substring without repe...

    xcold 評論0 收藏0
  • [LintCode] Longest Substring Without Repeating Cha

    摘要:哈希表法雙指針法只有當也就是時上面的循環(huán)才會結束,,跳過這個之前的重復字符再將置為 Problem Given a string, find the length of the longest substring without repeating characters. Example For example, the longest substring without repeat...

    Scliang 評論0 收藏0
  • [Leetcode]Longest Substring Without Repeating Char

    摘要:解題思路本題借助實現(xiàn)。如果字符未出現(xiàn)過,則字符,如果字符出現(xiàn)過,則維護上次出現(xiàn)的遍歷的起始點。注意點每次都要更新字符的位置最后返回時,一定要考慮到從到字符串末尾都沒有遇到重復字符的情況,所欲需要比較下和的大小。 Longest Substring Without Repeating CharactersGiven a string, find the length of the lon...

    awesome23 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<