1ms JAVA accepted Code


  • 0
    I

    public class Solution {
    public int[] plusOne(int[] digits) {
    int l=digits.length;
    digits[l-1]++;
    if(digits[l-1]>9){
    int carry=0;
    for(int i=l-1;i>=0;i--){
    carry=digits[i]/10;
    if(carry==0){
    break;
    }
    if(i!=0)
    {
    digits[i]=digits[i]%10;
    digits[i-1]=digits[i-1]+carry;
    }
    }

          if(digits[0]>9){
        	int dupdigit[]=new int[digits.length+1];
        	dupdigit[0]=1;
        	for(int i=digits.length-1;i>0;i--){
        		dupdigit[i]=digits[i]%10;
        	}
        	digits= new int[dupdigit.length];
        	digits=dupdigit;
        }
        }
        return digits;
    }
    

    }


Log in to reply
 

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