
Algorithm Classification : Array
Difficulty Level : easy
Source : LeetCode - 66. Plus One



Add '1' to the last index of the array.
If the value at that index is 10, add '1' to the index one
position before it.Repeat this process.
If the first index is '10', increase the size of the array.
class Solution {
public int[] plusOne(int[] digits) {
for(int i=0;i< digits.length;i++) {
if(++digits[digits.length-i-1]!=10)
return digits;
digits[digits.length-i-1]=0;
}
digits = new int[digits.length+1];
digits[0] = 1;
return digits;
}
}


digits = new int[digits.length + 1];
I've recently discovered a method for expanding the size of an array by adding elements at the beginning, following the approach outlined earlier.