Clean Java Code, streaming parsing, beats 96.86%


  • 0
    J
    public class StringIterator {
    
        StringBuilder remain;
        char c;
        int count;
        public StringIterator(String compressedString) {
            remain = new StringBuilder(compressedString);
            c=' ';
            count = 0;
        }
        
        public char next() {
            if(!hasNext()) {
                return ' ';
            }
            count--;
            return c;
        }
        
        public boolean hasNext() {
            if(count!=0)
                return true;
            if(remain.length()!=0){
                c = remain.charAt(0);
                count = 0;
                int i=1;
                for(;i<remain.length()&&remain.charAt(i)<='9'&&remain.charAt(i)>='0';i++){
                    count*=10;
                    count+=(int)(remain.charAt(i)-'0');
                }
                remain = new StringBuilder(remain.substring(i)); 
            }
            return count!=0;
        }
    }
    
    /**
     * Your StringIterator object will be instantiated and called as such:
     * StringIterator obj = new StringIterator(compressedString);
     * char param_1 = obj.next();
     * boolean param_2 = obj.hasNext();
     */
    

Log in to reply
 

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