← 返回 oracle 的题目列表Add One to a Number Represented by Digits Without ArrayList
类型:online_judge
Given the decimal digit representation of a non-negative integer, where digits[0] is the most significant digit and every element is in [0, 9], add one to the number and return the resulting digit array.
Restriction: do not use Java ArrayList; use a fixed-size array and allocate a new array only when necessary.
Correctly handle carries and these edge cases:
all digits are 9;
a single digit;
consecutive 9s in the middle or at the end;
leading zeroes, if present, should preserve positional behavior.
Input Format
First line: integer n
Second line: n integers representing digits
Output Format
Print the resulting digits separated by spaces.
Constraints
1 <= n <= 2 * 10^5
0 <= digits[i] <= 9
Example
Input
3
1 2 9
Output
1 3 0