|
| 1 | +/* |
| 2 | +For a non-negative integer X, |
| 3 | +the array-form of X is an array of its digits in left to right order. |
| 4 | +For example, if X = 1231, then the array form is [1,2,3,1]. |
| 5 | + |
| 6 | +Given the array-form A of a non-negative integer X, return the array-form of the integer X+K. |
| 7 | + |
| 8 | +Example 1: |
| 9 | +Input: A = [1,2,0,0], K = 34 |
| 10 | +Output: [1,2,3,4] |
| 11 | +Explanation: 1200 + 34 = 1234 |
| 12 | + |
| 13 | +Example 2: |
| 14 | +Input: A = [2,7,4], K = 181 |
| 15 | +Output: [4,5,5] |
| 16 | +Explanation: 274 + 181 = 455 |
| 17 | + |
| 18 | +Example 3: |
| 19 | +Input: A = [2,1,5], K = 806 |
| 20 | +Output: [1,0,2,1] |
| 21 | +Explanation: 215 + 806 = 1021 |
| 22 | + |
| 23 | +Example 4: |
| 24 | +Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1 |
| 25 | +Output: [1,0,0,0,0,0,0,0,0,0,0] |
| 26 | +Explanation: 9999999999 + 1 = 10000000000 |
| 27 | +*/ |
| 28 | + |
| 29 | + |
| 30 | +class Solution { |
| 31 | + public List<Integer> addToArrayForm(int[] A, int K) { |
| 32 | + List<Integer> result = new ArrayList<>(); |
| 33 | + int digit; |
| 34 | + int X = 0; |
| 35 | + |
| 36 | + for (int i = 0; i < A.length; i++) { |
| 37 | + digit = A[i]; |
| 38 | + X = X * 10 + digit; |
| 39 | + } |
| 40 | + |
| 41 | + X = X + K; |
| 42 | + |
| 43 | + do { |
| 44 | + result.add(0, X % 10); |
| 45 | + X /= 10; |
| 46 | + } while (X > 0); |
| 47 | + |
| 48 | + return result; |
| 49 | + } |
| 50 | +} |
0 commit comments