My simple java solution


  • 0
    J
    public class Solution {
        public int[] plusOne(int[] digits) {
            if(digits == null) return digits;
            
            int carry = 0;
            for(int i = digits.length - 1; i >= 0; i--){
                int value = digits[i] + 1;
                digits[i] = value%10;
                carry = value/10;
                if(carry == 0){
                    break;
                }
            }
            if(carry == 1){
                int[] result = new int[digits.length + 1];
                result[0] = 1;
                for(int i = 0; i < digits.length; i++){
                    result[i+1] = digits[i];
                }
                return result;
            }
            return digits;
        }
    }

Log in to reply
 

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