- Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathPlusOne.java
More file actions
Latest commit
19 lines (19 loc) · 512 Bytes
/
Copy pathPlusOne.java
File metadata and controls
19 lines (19 loc) · 512 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/** Given a number represented as an array of digits, plus one to the number. */
publicclassPlusOne {
publicint[] plusOne(int[] digits) {
intlength = digits.length;
intadd = 1;
for (inti = length - 1; i >= 0; i--) {
intsum = digits[i] + add;
digits[i] = sum % 10;
add = sum / 10;
if (add == 0) returndigits;
}
int[] ret = newint[length + 1];
ret[0] = add;
for (inti = 0; i < length; i++) {
ret[i + 1] = digits[i];
}
returnret;
}
}