亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

316. Remove Duplicate Letters

chunquedong / 2431人閱讀

摘要:以及枚舉的做法,因為這題只有個字母,枚舉的復(fù)雜度是,參考博客還有先把排序,然后從小到大取字母的寫法,參考

316. Remove Duplicate Letters

題目鏈接:https://leetcode.com/problems...

用一個stack來做,stack里面的字母按增序來排,出現(xiàn)top>cur的時候要把top給pop出來,注意一點是如果后面沒有top的話,就不能pop出來,所以先要用個hashmap來保存每個字母出現(xiàn)的次數(shù),次數(shù)到0說明后面沒有這個字母了,還需要一個visited數(shù)組來保存已經(jīng)在stack里面的字母

pop: while top > cur && map[top] > 0

push: when !visited[cur]

loop invariant是(0, i)是到i為止最小的字母order結(jié)果。

public class Solution {
    public String removeDuplicateLetters(String s) {
        // count the number of each character
        int[] map = new int[26];
        for(char c : s.toCharArray()) {
            map[c - "a"]++;
        }
        // stack to store the smallest lexi order
        char[] stack = new char[s.length()];
        // visited to record already added letter
        boolean[] visited = new boolean[26];
        int i = 0;
        for(char c : s.toCharArray()) {
            map[c - "a"]--;
            if(visited[c - "a"]) continue;
            // pop top character if it is > c && there are remains in the right
            while(i > 0 && stack[i-1] > c && map[stack[i-1] - "a"] > 0) {
                visited[stack[i-1] - "a"] = false;
                i--;
            }
            stack[i++] = c;
            visited[c-"a"] = true;
        }
        
        return new String(stack, 0, i);
    }
}

以及枚舉的做法,因為這題只有26個字母,枚舉的復(fù)雜度是O(26*N),參考博客:
http://bookshadow.com/weblog/...

還有先把string排序,然后從小到大取字母的寫法,參考discussion:
https://discuss.leetcode.com/...

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

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/76400.html

相關(guān)文章

  • LeetCode[316] Remove Duplicate Letters

    摘要:復(fù)雜度思路用一個每次考慮當(dāng)前的字符大小和的頂端字符的大小,如果當(dāng)前字符比較小的話,則可以出頂端的字符,將當(dāng)前的字符放進中。需要維持了一個判斷當(dāng)前字符在剩余字符串中的出現(xiàn)次數(shù),考慮能否將這個字符從棧中彈出。 LeetCode[316] Remove Duplicate Letters Given a string which contains only lowercase letter...

    tomorrowwu 評論0 收藏0
  • 316. Remove Duplicate Letters and 402. Remove K Di

    摘要:每個字母只留一個,而且保證字典序最小。從前往后掃描,還要往前看一個檢出是否刪除的,要用解。需要一個數(shù)據(jù)結(jié)構(gòu)記錄是否使用這個字母,可以用。結(jié)構(gòu)也可以用數(shù)組加頂點指針模擬。 316 Remove Duplicate Given a string which contains only lowercase letters, remove duplicate letters so that e...

    novo 評論0 收藏0
  • [LeetCode]Remove Duplicate Letters

    摘要:首先要求就是得保證在原來的字符串中存在當(dāng)前字符的順序,即要保證每次的字符的次數(shù)大于。讀字符的過程中,把字符存到里,當(dāng)發(fā)現(xiàn)之前存的字符中比當(dāng)前字符大而且頻率還大于就可以把那個字符出去?;舅枷刖褪窃谝欢ǖ南拗茥l件下出比當(dāng)前選擇差的元素。 Remove Duplicate Letters Given a string which contains only lowercase lette...

    gaosboy 評論0 收藏0
  • [LintCode/LeetCode] Remove Duplicate Letters

    Problem Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical...

    wanghui 評論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)-鏈表

    摘要:猶太士兵決定寧可自殺也不做俘虜,于是商量出了一個自殺方案。他們圍成一個圈,從一個人開始,數(shù)到第三個人時將第三個人殺死,然后再數(shù),直到殺光所有人。使用循環(huán)鏈表解決該問題。首先我們看到他們圍成一個圈判斷應(yīng)該使用循環(huán)鏈表來處理改問題完整代碼前移 本章將討論另一種列表: 鏈表 . 解釋為什么有時鏈表優(yōu)于數(shù)組, 還會實現(xiàn)一個基于對象的鏈表. 數(shù)組的缺點 數(shù)組不總是組織數(shù)據(jù)的最佳數(shù)據(jù)結(jié)構(gòu), 原因如...

    stormzhang 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<