Java Solution without using Queue


  • 0
    J
    public class StringIterator {
        char [] chars;
        int [] nums;
        int curindex;
        public StringIterator(String compressedString) {
            char [] c = compressedString.toCharArray();
            chars = new char[c.length/2];
            nums = new int[c.length/2];
            int ind =0;
            for(int i=0;i<c.length;){
                chars[ind] = c[i];
                i++;
                System.out.println(Character.isDigit(c[i]));
                nums[ind] =c[i] -'0';
                i++;
                while(i < c.length && Character.isDigit(c[i])){
                    int v = c[i] -'0';
                    nums[ind] = nums[ind]*10 + v;
                    i++;
                }
                ind++;
            }
            curindex = 0;
        }
        
        public char next() {
            if(hasNext()){
                nums[curindex]--;
                return chars[curindex];
            }
            else{
                return ' ';
            }
        }
        
        public boolean hasNext() {
            if(curindex >= nums.length){
                return false;
            }
            while(curindex < nums.length && nums[curindex] == 0){
                curindex++;
            }
            return curindex < nums.length;
        }
    }
    
    /**
     * 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.