1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public class PlusOne { public int[] plusOne(int[] digits) { if (digits.length == 0) { return new int[]{1}; } int inc = 1; for (int i = digits.length - 1; i >= 0; i--) { int sum = inc + digits[i]; digits[i] = sum % 10; inc = sum / 10; if (inc == 0) { break; } } if (inc == 1) { int[] result = new int[digits.length + 1]; result[0] = 1; System.arraycopy(digits, 0, result, 1, digits.length); return result; } return digits; } }
|