/**
 * Given a non-negative number represented as an array of digits,
 * <strong>plus one</strong> to the number.
 *
 * The digits are stored such that the most significant digit is
 * at the head of the list.
 *
 * Tags: Array, Math
 */
class PlusOne {
    public static void main(String[] args) {
        int[] result = plusOne(new int[]{9, 9, 9, 9, 8});
        System.out.print("{ ");
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
        System.out.println("}");
    }

    /**
     * Add 1 for the last digit
     * If carry, add one to next digit
     * If no carry, return
     * If all have carry, create a new int array
     */
    public int[] plusOneOthers(int[] digits) {
        for(int i = digits.length - 1; i >= 0; i--){
            digits[i] = 1 + digits[i];
            if (digits[i] == 10) {
                digits[i] = 0;
            } else return digits; // no carry for current digit
        }

        int[] ans = new int[digits.length + 1];
        ans[0] = 1;
        for(int i = 0;i < digits.length; i++) ans[i + 1] = digits[i];
        return ans;
    }

    public static int[] plusOne(int[] digits) {
        int count = digits.length;
        while (count > 0) {
            digits[count - 1] = digits[count - 1] + 1;
            if (digits[count - 1] > 9) {
                digits[count - 1] = 0;
            } else {
                return digits;
            }
            count--;
        }
        int[] result = new int[digits.length + 1];
        result[0] = 1;
        for (int i = 1; i < digits.length; i++) {
            result[i] = digits[i - 1];
        }

        return result;
    }
}

@@ -1,12 +1,62 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0A
 /**%0A * Given
@@ -116,37 +116,17 @@
 its,
-%0A * %3Cstrong%3Eplus one%3C/strong%3E
+ plus one
  to 
@@ -135,24 +135,28 @@
 e number.%0A *
+ %3Cp%3E
 %0A * The digi
@@ -208,19 +208,16 @@
 digit is
-%0A *
  at the 
@@ -236,16 +236,44 @@
 list.%0A *
+ %3Cp%3E%0A * Company Tags: Google
 %0A * Tags
@@ -283,17 +283,16 @@
 rray
-,
  Math%0A *
 /%0Acl
@@ -291,470 +291,117 @@
 h%0A *
-/%0Aclass PlusOne %7B%0A    public static void main(String%5B%5D args) %7B%0A        int%5B%5D result = plusOne(new int%5B%5D%7B9, 9, 9, 9, 8%7D);%0A        System.out.print(%22%7B %22);%0A        for (int i = 0; i %3C result.length; i++) %7B%0A            System.out.print(result%5Bi%5D + %22 %22);%0A        %7D%0A        System.out.println(%22%7D%22);%0A    %7D%0A%0A    /**%0A     * Add 1 for the last digit%0A     * If carry, add one to next digit%0A     * If no carry, return%0A     * If all have carry, create a new int array%0A     */
+ Similar Problems: (M) Multiply Strings, (E) Add Binary, (M) Plus One Linked List%0A */%0Apublic class PlusOne %7B%0A
 %0A   
@@ -425,14 +425,8 @@
 sOne
-Others
 (int
@@ -441,32 +441,33 @@
 s) %7B%0A        for
+ 
 (int i = digits.
@@ -490,16 +490,17 @@
  0; i--)
+ 
 %7B%0A      
@@ -562,24 +562,34 @@
 %5Bi%5D == 10) %7B
+ // Carry.
 %0A           
@@ -631,52 +631,66 @@
 lse 
-return digits;
+%7B
  // 
-n
+N
 o carry
- for current
+, just return.%0A                return
  digit
+s;
 %0A   
@@ -686,34 +686,89 @@
 digits;%0A        
-%7D%0A
+    %7D%0A        %7D%0A        // Not returned, must have carry.
 %0A        int%5B%5D a
@@ -831,16 +831,17 @@
      for
+ 
 (int i =
@@ -843,16 +843,17 @@
 t i = 0;
+ 
 i %3C digi
@@ -867,16 +867,30 @@
 th; i++)
+ %7B%0A           
  ans%5Bi +
@@ -898,32 +898,42 @@
 1%5D = digits%5Bi%5D;%0A
+        %7D%0A
         return a
@@ -958,15 +958,8 @@
 lic 
-static 
 int%5B
@@ -959,32 +959,33 @@
 ic int%5B%5D plusOne
+B
 (int%5B%5D digits) %7B
@@ -1449,25 +1449,24 @@
 ;%0A        %7D%0A
-%0A
         retu

update plus one