LeetCode 604. Design Compressed String Iterator(java)

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 ' '
思路:首先,我想的是用一个stack,从后往前,把最后生成的字符都塞进stack,然后每次next()和hasNext()的时候,就从stack中pop出来。但是这个方法过不了leetcode,因为出现了这样一个例子“a1038373845b1032394854”,导致时间非常慢,所以我又换了一个方法,从左边开始,每次pop都计数-1,当计数为0了,再开始第二个字符。以下为两个方法的代码,但只有第二个方法可以过LeetCode。
//代码一:stack
class StringIterator {

    Stack stack = new Stack<>();

    public StringIterator(String compressedString) {
        int count = 1, num = 0, len = compressedString.length();
        for (int i = len - 1; i >= 0; i--) {
            char c = compressedString.charAt(i);
            if (Character.isDigit(c)) {
                int end = i;
                while (i < len && Character.isDigit(compressedString.charAt(i))) {
                    i--;
                }
                i++;
                for (int j = i; j <= end; j++) {
                    num = num * 10 + compressedString.charAt(j) - '0';
                    if (num > Integer.MAX_VALUE) {
                        num = Integer.MAX_VALUE;

                    }
                }
                count = num;
                num = 0;
            } else {
                for (int j = 0; j < count; j++) {
                    stack.push(c);
                }
                count = 1;
            }
        }
    }

    public char next() {
        return stack.isEmpty() ? ' ' : stack.pop();
    }

    public boolean hasNext() {
        return stack.isEmpty() ? false : true;
    }
}
//代码二:
class StringIterator {
    char letter;
    int count;
    int index;
    int len;
    String s;
    public StringIterator(String compressedString) {
        s = compressedString;
        len = s.length();
        index = 0;
        helper();
    }

    public char next() {
        if (count > 0) {
            count--;
            return letter;
        } else {
            if (index == len) {
                return ' '; 
            } else {
                helper();
                count--;
                return letter;
            }
        }
    }

    public boolean hasNext() {
        return (index == len && count <= 0) ? false : true;
    }

    public void helper() {
        char c = s.charAt(index);
        letter = c;
        int num = 0;
        while (++index < len && Character.isDigit(s.charAt(index))) {
            num = num * 10 + s.charAt(index) - '0';
        }
        count = num;
    }
}

你可能感兴趣的:(String,java,leetcode)