MCPcopy Create free account
hub / github.com/DestinationFAANG/Destination-FAANG-Java-Solution / Solution

Class Solution

66 Plus One/66 Plus One.java:10–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8**************** Java Solution ***********************
9
10class 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected