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

資訊專欄INFORMATION COLUMN

[LintCode] 604. Design Compressed String Iterator

alin / 561人閱讀

Problem

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.

The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.

next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space.
hasNext() - Judge whether there is any letter needs to be uncompressed.

Note:
Please remember to RESET your class variables declared in StringIterator, as static/class variables are persisted across multiple test cases. Please see here for more details.

Example:

StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");

iterator.next(); // return "L"
iterator.next(); // return "e"
iterator.next(); // return "e"
iterator.next(); // return "t"
iterator.next(); // return "C"
iterator.next(); // return "o"
iterator.next(); // return "d"
iterator.hasNext(); // return true
iterator.next(); // return "e"
iterator.hasNext(); // return false
iterator.next(); // return " "
Solution
class StringIterator {
    Queue queue;
    public StringIterator(String str) {
        queue = new LinkedList<>();
        int i = 0, len = str.length();
        while (i < len) {
            int j = i+1;
            while (j < len && Character.isDigit(str.charAt(j))) j++;
            
            char ch = str.charAt(i);
            int count = Integer.parseInt(str.substring(i+1, j));
            queue.offer(new Node(ch, count));
            
            i = j;
        }
    }
    public char next() {
        if (!hasNext()) return " ";
        Node node = queue.peek();
        node.count--;
        if (node.count == 0) queue.poll();
        return node.val;
    }
    public boolean hasNext() {
        return !queue.isEmpty();
    }
}
class Node {
    char val;
    int count;
    public Node(char c, int n) {
        this.val = c;
        this.count = n;
    }
}

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

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

相關(guān)文章

  • [LintCode] Binary Search Tree Iterator

    摘要:建立一個(gè)堆棧,先將最左邊的結(jié)點(diǎn)從大到小壓入棧,這樣的話,為了實(shí)現(xiàn)迭代即返回下一個(gè)的函數(shù)就要考慮右邊的結(jié)點(diǎn)。如此,實(shí)現(xiàn)函數(shù)。 Problem Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-o...

    silencezwm 評(píng)論0 收藏0
  • [LintCode] String Compression

    String Compression Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the compressed string w...

    Cruise_Chan 評(píng)論0 收藏0
  • [LintCode/LeetCode] Flatten Nested List Iterator

    摘要:首先,根據(jù)迭代器需要不斷返回下一個(gè)元素,確定用堆棧來做。堆棧初始化數(shù)據(jù)結(jié)構(gòu),要先從后向前向堆棧壓入中的元素。在調(diào)用之前,先要用判斷下一個(gè)是還是,并進(jìn)行的操作對(duì)要展開并順序壓入對(duì)直接返回。 Problem Given a nested list of integers, implement an iterator to flatten it. Each element is either...

    spacewander 評(píng)論0 收藏0
  • [LeetCode/LintCode] Design Twitter/Mini Twitter

    摘要:首先建立按時(shí)間戳從大到小排列的,找到中的,出其中在中存在的,把每一個(gè)的推特鏈表放入,再?gòu)闹腥☆^十條推特的放入結(jié)果數(shù)組。 Design Twitter Note 建立兩個(gè)HashMap,一個(gè)存user,一個(gè)存tweets。以及整型的時(shí)間戳timestamp。user的k-v pair是userId-follower_set,tweets的k-v pair是userId-tweets_li...

    honmaple 評(píng)論0 收藏0
  • Design Patterns - Iterator Pattern(譯)

    摘要:迭代器模式屬于行為型模式下的一種。實(shí)現(xiàn)我們將創(chuàng)建一個(gè)接口,該接口描述迭代所需要的方法緊接著聲明了一個(gè)接口,該接口返回一個(gè)對(duì)象。我們會(huì)創(chuàng)建具體的類實(shí)現(xiàn)接口和接口,并去使用它們。第三步使用獲得迭代器并且打印。 原文地址譯者 smallclover希望對(duì)你們有所幫助 設(shè)計(jì)模式-迭代器模式 迭代器是Java和.Net程序環(huán)境下經(jīng)常使用的一種設(shè)計(jì)模式。這種設(shè)計(jì)模式通常用來獲取能順序訪問集合對(duì)元素...

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

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

0條評(píng)論

alin

|高級(jí)講師

TA的文章

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