66 Plus One – Easy
Problem:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Thoughts:
This is a little tricky question. The interface requires to return int[], but you are not sure what’s the length for the returning array. Given input array, int[] digits, it could be digits.length or digits.length + 1.
Solutions:
This solution will avoid unnecessary calculation in the first solution.
when the number given has a lot of 9, then there is no need to do these calculation. Because when you initialize an array of integer in Java, it has default value of 0 already.
Last updated