国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 159. Longest Substring with At Most Two

geekidentity / 2854人閱讀

Problem

Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.

Example 1:

Input: "eceba"
Output: 3
Explanation: t is "ece" which its length is 3.
Example 2:

Input: "ccaabbb"
Output: 5
Explanation: t is "aabbb" which its length is 5.

Solution
class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s == null || s.length() == 0) return 0;
        Map map = new HashMap<>();
        int i = 0, j = 0, len = s.length(), max = 0;
        char[] str = s.toCharArray(); //char[] is much faster
        while (j < len) {
            map.put(str[j], j);
            j++;
            if (map.size() > 2) {
                int leftMost = len;
                for (int index: map.values()) leftMost = Math.min(leftMost, index);
                map.remove(str[leftMost]);
                i = leftMost+1;
            }
            max = Math.max(max, j-i);
        }
        return max;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72588.html

相關(guān)文章

  • 159. Longest Substring with At Most Two Distinct C

    摘要:表示某個(gè)最后一次出現(xiàn)的地方可能只包含一種或者兩種只包含一種強(qiáng)制保持出現(xiàn)兩種保證,為了計(jì)算方便出現(xiàn)第三種的時(shí)候,直接計(jì)算出當(dāng)前長(zhǎng)度。 Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = e...

    liujs 評(píng)論0 收藏0
  • 159. Longest Substring With At Most Two Distinct C

    摘要:題目解法最重要的是把最后一次出現(xiàn)的這個(gè)的記在的里面。所以當(dāng)出現(xiàn)不止兩個(gè)的數(shù)的時(shí)候,把這個(gè)最低的刪掉,把最的加就可以啦 題目:Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = eceba...

    spacewander 評(píng)論0 收藏0
  • [Leetcode] Longest Substring with At Most 2 Distin

    摘要:最新思路解法哈希表法復(fù)雜度時(shí)間空間思路我們遍歷字符串時(shí)用一個(gè)哈希表,但這個(gè)哈希表只記錄兩個(gè)東西,一個(gè)字母和它上次出現(xiàn)的時(shí)的下標(biāo),另一個(gè)字母和它上次出現(xiàn)時(shí)候的下標(biāo)。這個(gè)通過(guò)用哈希表記錄字母上次出現(xiàn)的下標(biāo),來(lái)維護(hù)一個(gè)窗口的方法也可以用于。 Longest Substring with At Most Two Distinct Characters 最新思路解法:https://yanjia...

    imccl 評(píng)論0 收藏0
  • [leetcode] Minimum Window Substring

    摘要:使用而不是因?yàn)槲覀冃枰氖亲钪担虚g值我們不在乎,所以一次收斂到最小。下面來(lái)三個(gè)需要查重并且記錄上次出現(xiàn)的位置,選擇以為例,走到用做檢查,發(fā)現(xiàn)出現(xiàn)過(guò),把移到的下一個(gè)。是上個(gè)題目的簡(jiǎn)易版,或者特殊版。 這里聊一聊解一類問(wèn)題,就是滿足某一條件的substring最值問(wèn)題。最開(kāi)始我們以Minimum Window Substring為例,并整理總結(jié)leetcode里所有類似題目的通解。 Gi...

    Pines_Cheng 評(píng)論0 收藏0
  • [Leetcode] Substring with Concatenation of All Wor

    摘要:每次搜索中,我們通過(guò)哈希表維護(hù)一個(gè)窗口,比如中,我們先拿出。如果都不在數(shù)組中,那說(shuō)明根本不能拼進(jìn)去,則哈希表全部清零,從下一個(gè)詞開(kāi)始重新匹配。 Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are all of the same...

    adie 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<