@@ -0,0 +1,1624 @@
+package in.xnnyygn.algorithm.leetcode;%0A%0Aimport java.util.Arrays;%0A%0A/**%0A * Problem: Plus One. %3Ca href=%22https://leetcode.com/problems/plus-one/%22%0A * target=%22_blank%22%3ELink%3C/a%3E.%0A * %0A * @author xnnyygn%0A */%0Apublic class PlusOne %7B%0A%0A  public int%5B%5D plusOne(int%5B%5D digits) %7B%0A    Tuple2%3Cint%5B%5D, Boolean%3E result = plusOne1(digits);%0A    if (result._2) %7B // has 1 at first%0A      int len = digits.length;%0A      int%5B%5D newArray = new int%5Blen + 1%5D;%0A      System.arraycopy(result._1, 0, newArray, 1, len);%0A      newArray%5B0%5D = 1;%0A      return newArray;%0A    %7D%0A    return result._1;%0A  %7D%0A%0A  class Tuple2%3CT1, T2%3E %7B%0A    T1 _1;%0A    T2 _2;%0A%0A    public Tuple2(T1 _1, T2 _2) %7B%0A      this._1 = _1;%0A      this._2 = _2;%0A    %7D%0A  %7D%0A%0A  private Tuple2%3Cint%5B%5D, Boolean%3E plusOne1(int%5B%5D digits) %7B%0A    boolean flag = true; // use flag to simulate adding one%0A    for (int i = digits.length - 1; i %3E= 0; i--) %7B%0A      if (!flag) break;%0A      int n = digits%5Bi%5D + 1;%0A      if (n %3E 9) %7B%0A        digits%5Bi%5D = 0;%0A      %7D else %7B%0A        digits%5Bi%5D = n;%0A        flag = false;%0A      %7D%0A    %7D%0A    return new Tuple2%3Cint%5B%5D, Boolean%3E(digits, flag);%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    PlusOne soluton = new PlusOne();%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B0%7D)));%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B1%7D)));%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B9%7D)));%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B1, 1%7D)));%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B9, 1%7D)));%0A    System.out.println(Arrays.toString(soluton.plusOne(new int%5B%5D %7B9, 9%7D)));%0A  %7D%0A%0A%7D%0A

add Plus One