Not Bad Java Solution


  • 0
    D
    public class Solution {
        public int[] plusOne(int[] digits) {
            int len = digits.length;
            int carry =1;
            for (int i=len; i>0; i--){
                int index = i-1;
                int current = digits[index];
                int result = current + carry;
                if ( result > 9){
                    digits[index] = result % 10;
                    carry = 1;
                    if (index==0 && carry==1){
                        int[] a = digits;
                        digits = new int[len+1];
                        digits[0]=1;
                        for (int j=1;j<digits.length;j++){
                            digits[j]=a[j-1];
                        }
                    }
                }
                else{
                    digits[index] = result;
                    carry = 0;
                }
            }
            return digits;
            
        }
    }

Log in to reply
 

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