1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Solution { public int[] plusOne(int[] digits) { if (digits == null || digits.length == 0) return new int[0]; int[] result = new int[digits.length]; int carry = 1; for (int i = digits.length-1; i >= 0; i--){ result[i] = (digits[i]+carry)%10; carry = (digits[i]+carry)/10; } if (carry == 1){ result = new int[result.length+1]; result[0] = 1; } return result; } } |
No comments:
Post a Comment