Skip to content

Commit 250a089

Browse files
committed
added Add to Array-Form of Integer (easy)
1 parent b88e937 commit 250a089

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Easy.AddToArrayFormInteger;
2+
3+
import java.util.Arrays;
4+
5+
public class Driver {
6+
7+
public static void main(String[] args) {
8+
Solution sol = new Solution();
9+
int[] A = {1, 2};
10+
int K = 298;
11+
System.out.println("Input: A = " + Arrays.toString(A) + ", K = " + K );
12+
System.out.println("Output: " + sol.addToArrayForm(A, K).toString());
13+
}
14+
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
# Add to Array-Form of Integer
3+
[Leetcode Link](https://leetcode.com/problems/add-to-array-form-of-integer/)
4+
5+
## Problem:
6+
7+
For a non-negative integer `X`, the *array-form* of `X` is an array of its digits in left to right order. For example, if `X = 1231`, then the array form is `[1,2,3,1]`.
8+
9+
Given the array-form A of a non-negative integer `X`, return the array-form of the integer `X+K`.
10+
11+
## Example:
12+
13+
```
14+
Input: A = [1,2,0,0], K = 34
15+
Output: [1,2,3,4]
16+
Explanation: 1200 + 34 = 1234
17+
```
18+
```
19+
Input: A = [2,7,4], K = 181
20+
Output: [4,5,5]
21+
Explanation: 274 + 181 = 455
22+
```
23+
```
24+
Input: A = [2,1,5], K = 806
25+
Output: [1,0,2,1]
26+
Explanation: 215 + 806 = 1021
27+
```
28+
```
29+
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
30+
Output: [1,0,0,0,0,0,0,0,0,0,0]
31+
Explanation: 9999999999 + 1 = 10000000000
32+
```
33+
34+
## Note:
35+
36+
- `1 <= A.length <= 10000`
37+
- `0 <= A[i] <= 9`
38+
- `0 <= K <= 10000`
39+
- If `A.length > 1`, then `A[0] != 0`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Easy.AddToArrayFormInteger;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
8+
public List<Integer> addToArrayForm(int[] A, int K) {
9+
// convert int[] into List<Integer> to deal with addition overflow (increase in size)
10+
List<Integer> result = new ArrayList<>(A.length);
11+
for (int digit: A) {
12+
result.add(digit);
13+
}
14+
// System.out.println(result.toString());
15+
16+
// start from right-end add corresponding digit from K
17+
for (int i = result.size()-1; i >=0; i--) {
18+
int sum = result.get(i) + (K%10);
19+
K /= 10;
20+
// if sum overflows (>= 10), then add abudant to K, and only take the one's digit as final sum
21+
if (sum >= 10) {
22+
K += sum/10;
23+
sum %= 10;
24+
}
25+
// System.out.println(sum);
26+
result.set(i, sum);
27+
}
28+
// deal with the remaining K
29+
while (K > 0) {
30+
result.add(0, K%10);
31+
K /= 10;
32+
}
33+
return result;
34+
}
35+
36+
}

Hard/MaximumScoreWords/Solution.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package Hard.MaximumScoreWords;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collections;
64
import java.util.HashMap;
75
import java.util.List;
86
import java.util.Map;

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Languages used: Java and Python
3434
- [Isomorphic Strings](Easy/IsomorphicStrings)
3535
- [Binary Prefix Divisible By 5](Easy/BinaryPrefixDivBy5)
3636
- [Rank Transform of an Array](Easy/RankTransformArray)
37+
- [Add to Array-Form of Integer](Easy/AddToArrayFormInteger)
3738
- Medium
3839
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
3940
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)