AC Concise Java Solution


  • 0
    A
    public int[] plusOne(final int[] digits) {
            int[] result = new int[digits.length + 1];
            
            int carry = 1;
            for (int i = digits.length - 1; i >= 0; i--) {
                int sum = digits[i] + carry;
                result[i + 1] = sum % 10;
                carry = sum / 10;
            }
            result[0] = carry;
            
            return result[0] != 0 ? result : Arrays.copyOfRange(result, 1, result.length);
        }
    

Log in to reply
 

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