My JAVA solution unique


  • 0
    G
    public class StringIterator {
        
        int charindex = 0;
        int intindex =1;
        int intindexstart =1;
        int totallen;
        //int len;
        int curused =1;
        int repeatTimes;
        
        String s;
        
        public StringIterator(String compressedString) {
            totallen = compressedString.length();
            s=compressedString;
            
        }
        
        public char next() {
            if(intindex<totallen){
                
                while(intindex+1<totallen && s.charAt(intindex+1)>='0' && s.charAt(intindex+1)<='9'){
                    intindex++;
                }
                
                
                repeatTimes = Integer.valueOf(s.substring(intindexstart,intindex+1));
                //System.out.println("repeatTimes="+repeatTimes);
                //System.out.println("curused="+curused);
                if(curused > repeatTimes){
                    return tiao();
                }
                else{
                    curused++;
                    return butiao();
                }
            }
            
            
            
            return ' ';
        }
        
        public boolean hasNext() {
            
            if(intindex>=totallen){
                return false;
            }
            if(intindex>=totallen-1 && curused>repeatTimes){
                return false;
            }
            return true;
        }
        
        public char tiao(){
            
            
            intindex += 2;
            intindexstart = intindex;
            charindex = intindex-1;
            curused=1;
            if(intindex>=totallen){
                return ' ';
            }
            curused++;
            return s.charAt(charindex);
        }
        
        public char butiao(){
            
            
            return s.charAt(charindex);
        }
        
        
    }
    
    /**
     * 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.