Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b59f60

Browse files
solves #2553: Separate the Digits in an Array in java
1 parent e2565d3 commit 2b59f60

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@
798798
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value) | [![Java](assets/java.png)](src/MinimumCommonValue.java) | |
799799
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum) | [![Java](assets/java.png)](src/AlternatingDigitSum.java) | |
800800
| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board) | [![Java](assets/java.png)](src/CountDistinctNumbersOnBoard.java) | |
801-
| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array) | | |
801+
| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array) | [![Java](assets/java.png)](src/SeparateTheDigitsInAnArray.java) | |
802802
| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile) | | |
803803
| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value) | | |
804804
| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit) | | |

‎src/SeparateTheDigitsInAnArray.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/separate-the-digits-in-an-array
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class SeparateTheDigitsInAnArray {
9+
public int[] separateDigits(int[] nums) {
10+
final List<Integer> digits = new ArrayList<>();
11+
for (int number : nums) {
12+
addDigitsToList(digits, number);
13+
}
14+
return toArray(digits);
15+
}
16+
17+
private int[] toArray(List<Integer> list) {
18+
final int[] array = new int[list.size()];
19+
for (int index = 0 ; index < list.size() ; index++) {
20+
array[index] = list.get(index);
21+
}
22+
return array;
23+
}
24+
25+
private void addDigitsToList(List<Integer> digits, int number) {
26+
final String num = number + "";
27+
for (int index = 0 ; index < num.length() ; index++) {
28+
digits.add(num.charAt(index) - '0');
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /