[C++] [Java] Clean Code


  • 5

    C++

    class StringIterator {
    public:
        StringIterator(string cs) : cs(cs), i(0), rep(0), ch(' ') { }
    
        char next() {
            if (rep > 0) {
                rep--;
                return ch;
            }
            else if (i < cs.size()) {
                ch = cs[i];
                int j = i + 1;
                while (j < cs.size() && isdigit(cs[j])) { j++; }
                string num = cs.substr(i + 1, j - 1 - i);
                rep = atoi(num.c_str()) - 1;
                i = j;
                return ch;
            }
            else {
                return ' ';
            }
        }
    
        bool hasNext() {
            return rep > 0 || i < cs.size();
        }
    
    private:
        string cs;
        int i;
        long rep;
        char ch;
    };
    

    Java

    public class StringIterator {
        private char[] cs;
        private int i;
        private long rep;
        private char ch;
    
        public StringIterator(String compressedString) {
            cs = compressedString.toCharArray();
        }
        
        public char next() {
            if (rep > 0) {
                rep--;
                return ch;
            }
            else if (i < cs.length) {
                ch = cs[i];
                int j = i + 1;
                while (j < cs.length && Character.isDigit(cs[j])) { j++; }
                String num = new String(cs, i + 1, j - 1 - i);
                rep = Integer.valueOf(num) - 1;
                i = j;
                return ch;
            }
            else {
                return ' ';
            }        
        }
        
        public boolean hasNext() {
            return rep > 0 || i < cs.length;
        }
    }
    

    Java2

    public class StringIterator {
        private int i;
        private long rep;
        private char ch;
        private char[] cs;
        public StringIterator(String compressedString) {
            cs = compressedString.toCharArray();
        }
        
        public char next() {
            if (rep > 0) {
                rep--;
                return ch;
            }
            else if (i < cs.length) {
                ch = cs[i];
                int j = i + 1;
                while (j < cs.length && Character.isDigit(cs[j])) {
                    rep = rep * 10 + cs[j] - '0';
                    j++;
                }
                rep--;
                i = j;
                return ch;
            }
            else {
                return ' ';
            }        
        }
        
        public boolean hasNext() {
            return rep > 0 || i < cs.length;
        }
    }
    

  • 0
    D

    Very nice. I tend to over complicate things. I preprocessed the string and stored the characters and frequencies in an array of pairs.


  • 0
    D
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.