| 8 | **************** Java Solution *********************** |
| 9 | |
| 10 | class Solution { |
| 11 | public static int[] plusOne(int[] digits) { |
| 12 | // Start from the last digit and move leftward |
| 13 | for (int i = digits.length - 1; i >= 0; i--) { |
| 14 | if (digits[i] < 9) { |
| 15 | digits[i]++; // No carry needed, just increment and return |
| 16 | return digits; |
| 17 | } |
| 18 | // If the digit is 9, set it to 0 and continue to the next digit |
| 19 | digits[i] = 0; |
| 20 | } |
| 21 | |
| 22 | // If all digits were 9, we need a new array with an extra digit |
| 23 | int[] newDigits = new int[digits.length + 1]; |
| 24 | newDigits[0] = 1; // The most significant digit is 1, rest are 0 by default |
| 25 | return newDigits; |
| 26 | } |
| 27 | |
| 28 | } |
nothing calls this directly
no outgoing calls
no test coverage detected