Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
[1, 2, 3] => 123
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You cannot use built-in functions such as int() and str().
SOLUTION: If you are going to solve this problem 3 edge cases should come into your mind.
Case 1: When there is no 9 digit in the array
Input: [1, 2, 3]
Output: [1, 2, 4]
Case 2: When the last element in the array is 9.
Input: [1, 2, 9]
Output: [1, 3, 0] Wrong Output: [1, 2, 10]
Case 3: When all the elements in the array are 9.
Input: [9, 9, 9]
Output: [1, 0, 0 ,0] Wrong Output: [9, 9, 10] , [0, 0, 1]
FINAL SOLUTION:
Increment an Arbitrary-Precision Integer Or Plus One
Reviewed by Leaf Code
on
August 08, 2020
Rating:

No comments: