Solution


  • 0
    S

    public String countAndSay(int n) {

        	java.math.BigInteger initialValue = new java.math.BigInteger("1");
             //long initialValue = 1;   
            for(int i = 1 ; i < n ; i ++){
                    initialValue = nextNum(initialValue);
            }
            return ""+initialValue;
        }
        
        public  java.math.BigInteger nextNum(java.math.BigInteger n){
        	//System.out.println("Input: "+ n);
            long mod = 0;
            int temp=0;
            int tempCount=1;
            String newNum="";    
            while(n.mod(new java.math.BigInteger("10")).intValue()>=1){ //n=11; n%10=1
                mod= n.mod(new java.math.BigInteger("10")).intValue(); //mod=1
                n = n.divide(new java.math.BigInteger("10"));
                
                while(n.mod(new java.math.BigInteger("10")).intValue() == mod){
                   tempCount++; 
                   n = n.divide(new java.math.BigInteger("10"));
                }
                //System.out.println("Temp val: "+ tempCount);
                newNum="" + tempCount+""+mod + newNum;
                //System.out.println("Output: "+newNum);
                tempCount = 1;
            }
            
            return new java.math.BigInteger(newNum);
        
        }

Log in to reply
 

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