Java- accepted solution


  • 0
    S
    enter code here
    

    public class Solution {
    public int[] plusOne(int[] digits) {

        int l = digits.length;
        boolean b=true; 
        for(int i=0;i<l;i++)
        {
            if(digits[i]!=9)
            b=false;
        }`enter code here`
        
        
        
        int[] ad=null;
        if(b)
        {
            ad = new int[l+1];
            ad[0]=1;
            for(int i=0;i<l;i++)
            {
                ad[i+1]=digits[i];
            }
              
            for(int i=l;i>0;i--)
            {
                if(ad[i]!=9)
                    ad[i]+=1;
                else
                    ad[i]=0;
            }
        }  
            
        else
        {
            ad = new int[l];
         
            for(int i=0;i<l;i++)
            {
                ad[i]=digits[i];
            }
        
            if(digits[l-1]!=9)
            {
                ad[l-1]+=1;
            }
            else
            {
            	boolean x = false;
                for(int i=l-1;i>=0;i--)
                {
                    if(ad[i]!=9 && ad[i+1]==0 && x==false)
                    {    ad[i]+=1;
                    	 x=true;
                    }
                    else if(ad[i]!=9 && ad[i+1]!=0)
                        ;
                    else if(i!=l-1)
                    {
                    	if(ad[i]==9 && ad[i+1]==0 && x==false)
                    		ad[i]=0;
                    	else if(ad[i]==9 && ad[i+1]!=0);
                    }   
                    
                    else if(ad[i]==9)
                        ad[i]=0;
                }
            }
        }
        return ad;
    }
    

    }


Log in to reply
 

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