Java solution


  • 0
    M

    class Solution {
    public int[] plusOne(int[] digits) {
    int a[]=new int [digits.length+1];
    int b[]=new int[digits.length];
    int count=0;
    for(int i=0;i<digits.length;i++){
    b[i]=digits[i];
    }

         for(int i=0;i<digits.length;i++){
        if(digits[i]==9)
        {
            count++;
        }
        }
        if(count==digits.length) {   
            a[0]=1;
           return a; 
        }
        
        
     
        int j = digits.length-1;
            if(digits[j]==9){
                while(digits[j]==9){
                    digits[j]=0;
                    j=j-1;  
                }
                digits[j]=digits[j]+1;
             
            }
        
        if(b[digits.length - 1] < 9){
     
                digits[digits.length - 1]=  digits[digits.length - 1] +1;
        
            }
       
        
       
        return digits;
            
        }
    }

Log in to reply
 

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