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

資訊專欄INFORMATION COLUMN

LeetCode[316] Remove Duplicate Letters

tomorrowwu / 3189人閱讀

摘要:復(fù)雜度思路用一個(gè)每次考慮當(dāng)前的字符大小和的頂端字符的大小,如果當(dāng)前字符比較小的話,則可以出頂端的字符,將當(dāng)前的字符放進(jìn)中。需要維持了一個(gè)判斷當(dāng)前字符在剩余字符串中的出現(xiàn)次數(shù),考慮能否將這個(gè)字符從棧中彈出。

LeetCode[316] Remove Duplicate Letters

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 order among all possible results.

Example:
Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "acdb"

ascii array

復(fù)雜度
O(N), O(N)

思路
用一個(gè)stack,每次考慮當(dāng)前的字符大小和stack的頂端字符的大小,如果當(dāng)前字符比較小的話,則可以poll出stack頂端的字符,將當(dāng)前的字符放進(jìn)stack中。需要維持了一個(gè)array判斷當(dāng)前字符在剩余字符串中的出現(xiàn)次數(shù),考慮能否將這個(gè)字符從棧中彈出。

代碼

public String removeDuplicateLetters(String s) {
    Stack stack = new Stack<>();
    int[] arr = new int[26];
    boolean[] visited = new boolean[26];
    for(int i = 0; i < s.length(); i ++) {
        arr[s.charAt(i) - "a"] ++;
    }
    //
    for(int i = 0; i < s.length(); i ++) {
        char ch = s.charAt(i);
        arr[ch - "a"] --;
        if(visited[ch - "a"]) continue;
        if(!stack.isEmpty() && ch < stack.peek()) {
            //
            while(!stack.isEmpty() && arr[stack.peek() - "a"] > 0 && ch < stack.peek()) {
                char temp = stack.pop();
                visited[temp - "a"] --;
            }
        }
        visited[ch - "a"] = true;
        stack.push(ch);
    }
    // build the string;
    StringBuilder builder = new StringBuilder();
    while(!stack.isEmpty()) {
        builder.append(stack.pop());
    }
    return builder.reverse().toString();
}

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

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/65267.html

相關(guān)文章

  • 316. Remove Duplicate Letters

    摘要:以及枚舉的做法,因?yàn)檫@題只有個(gè)字母,枚舉的復(fù)雜度是,參考博客還有先把排序,然后從小到大取字母的寫法,參考 316. Remove Duplicate Letters 題目鏈接:https://leetcode.com/problems... 用一個(gè)stack來做,stack里面的字母按增序來排,出現(xiàn)top>cur的時(shí)候要把top給pop出來,注意一點(diǎn)是如果后面沒有top的話,就不能po...

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

    摘要:每個(gè)字母只留一個(gè),而且保證字典序最小。從前往后掃描,還要往前看一個(gè)檢出是否刪除的,要用解。需要一個(gè)數(shù)據(jù)結(jié)構(gòu)記錄是否使用這個(gè)字母,可以用。結(jié)構(gòu)也可以用數(shù)組加頂點(diǎn)指針模擬。 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)前字符大而且頻率還大于就可以把那個(gè)字符出去。基本思想就是在一定的限制條件下出比當(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
  • [LeetCode] 609. Find Duplicate File in System

    Problem Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms o...

    sf190404 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<