Problem Statement
This is another LeetCode question I came across, where I need to add 1 to a number.
The number is represented as an integer array where each index position is a digit and therefore a number from 0-9. There are no leading zeros except for the number 0 itself.
The index position 0, that is arr[0] represents the MSB and the index position n-1, that is
arr[n-1] represents the LSB.
Thus, we need to add one to the LSB and return the number as an integer array. Here is the code which I wrote (it got accepted, passed all test cases) :
class Solution { public int[] plusOne(int[] digits) { int carry = 0, len = digits.length; int i = len - 1; if (len == 0) return digits; if (len == 1 && digits[0] < 9) { digits[0] += 1; return digits; } if (len == 1 && digits[0] == 9) { digits[0] = 0; digits = resize(digits, 2); return digits; } do { digits[i] += 1; if (digits[i] == 10) { digits[i] = 0; carry = 1; } else { carry = 0; return digits; } i--; } while (carry == 1 && i > -1); if (carry == 1) { digits = resize(digits, len + 1); carry = 0; } return digits; } private int[] resize(int[] arr, int size) { int[] copy = new int[size]; int i; copy[0] = 1; for (i = 1; i < size; i++) copy[i] = arr[i-1]; return copy; } }
Analysis
- Running Time : O(n) in the worst case
- Total Space : O(n) in the worst case
- Worst Case : When adding 1 to a m-digit number results in an (m+1) digit number
(eg. 999 + 1 = 1000)
Questions
- Is there a better way to solve this problem? (I mean, a different but more efficient algorithm than mine)
I got a performance of only 29% with this solution no matter how many times I submitted the code, so definitely my algorithm is inefficient - Instead of using a resizing array, should I be using an array list if I want better performance?
Can ArrayList handle the overflow condition better than resizing array? - Should I be using the resizing array technique at all?
Is it less efficient? - Is O(n) the best possible running time for the problem?
I don't see how we can do it O(log n) or less.
Further Notes
- Here is a link to the Github repository which has the above code with more comments: PlusOne.java