package leetcode;

import java.util.List;

/**
 * https://leetcode.com/problems/add-to-array-form-of-integer/
 */
public class Problem989 {
    public List<Integer> addToArrayForm(int[] A, int K) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem989 prob = new Problem989();
        System.out.println(prob.addToArrayForm(new int[]{1, 2, 0, 0}, 34)); // [1,2,3,4]
        System.out.println(prob.addToArrayForm(new int[]{2, 7, 4}, 181)); // [4,5,5]
        System.out.println(prob.addToArrayForm(new int[]{2, 1, 5}, 806)); // [1,0,2,1]
        System.out.println(prob.addToArrayForm(new int[]{9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, 1)); // [1,0,0,0,0,0,0,0,0,0,0]
    }
}

@@ -12,16 +12,74 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import j
@@ -93,16 +93,16 @@
 l.List;%0A
-
 %0A/**%0A * 
@@ -261,513 +261,1196 @@
     
-// TODO%0A        return null
+String kStr = Integer.toString(K);%0A        int%5B%5D k = new int%5BkStr.length()%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem989 prob = new Problem989();%0A        System.out.println(prob.addToArrayForm(new int%5B%5D%7B1, 2, 0, 0%7D, 34)); // %5B1,2,3,4%5D%0A        System.out.println(prob.addToArrayForm(new int%5B%5D%7B2, 7, 4%7D, 181)); // %5B4,5,5%5D%0A        System.out.println(prob.addToArrayForm(new int%5B%5D%7B2, 1, 5%7D, 806)); // %5B1,0,2,1%5D%0A        System.out.println(prob.addToArrayForm(new int%5B%5D%7B9, 9, 9, 9, 9, 9, 9, 9, 9, 9%7D, 1)); // %5B1,0,0,0,0,0,0,0,0,0,0%5D
+for (int i = 0; i %3C kStr.length(); i++) %7B%0A            k%5Bi%5D = kStr.charAt(i) - '0';%0A        %7D%0A        List%3CInteger%3E answer = new ArrayList%3C%3E();%0A        int i = A.length - 1;%0A        int j = k.length - 1;%0A        int carry = 0;%0A        while (i %3E= 0 && j %3E= 0) %7B%0A            int sum = A%5Bi%5D + k%5Bj%5D + carry;;%0A            if (sum %3E= 10) %7B%0A                carry = 1;%0A            %7D else %7B%0A                carry = 0;%0A            %7D%0A            answer.add(sum %25 10);%0A            i--;%0A            j--;%0A        %7D%0A        while (i %3E= 0) %7B%0A            int sum = A%5Bi%5D + carry;%0A            if (sum %3E= 10) %7B%0A                carry = 1;%0A            %7D else %7B%0A                carry = 0;%0A            %7D%0A            answer.add(sum %25 10);%0A            i--;%0A        %7D%0A        while (j %3E= 0) %7B%0A            int sum = k%5Bj%5D + carry;%0A            if (sum %3E= 10) %7B%0A                carry = 1;%0A            %7D else %7B%0A                carry = 0;%0A            %7D%0A            answer.add(sum %25 10);%0A            j--;%0A        %7D%0A        if (carry == 1) %7B%0A            answer.add(1);%0A        %7D%0A        Collections.reverse(answer);%0A        return answer;
 %0A   

Solve problem 989