Given an array of positive integers nums, return an arrayanswerthat consists of the digits of each integer innums _after separating them inthe same order they appear in _nums.
To separate the digits of an integer is to get all the digits it has in the same order.
For example, for the integer 10921, the separation of its digits is [1,0,9,2,1].
Input: nums =[13,25,83,77]Output: [1,3,2,5,8,3,7,7]Explanation:
- The separation of 13is[1,3].- The separation of 25is[2,5].- The separation of 83is[8,3].- The separation of 77is[7,7].answer =[1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
import java.util.*;
classSolution {
public List<Integer>separateDigits(int[] nums) {
List<Integer> ans =new ArrayList<>();
for (int x : nums) {
for (char c : String.valueOf(x).toCharArray()) ans.add(c -'0');
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funseparateDigits(nums: IntArray): List<Int> {
val ans = mutableListOf<Int>()
for (x in nums) {
for (c in x.toString()) ans.add(c - '0')
}
return ans
}
}
1
2
3
4
5
6
classSolution:
defseparateDigits(self, nums: list[int]) -> list[int]:
ans = []
for x in nums:
ans.extend(int(c) for c in str(x))
return ans
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnseparate_digits(nums: Vec<i32>) -> Vec<i32> {
letmut ans =vec![];
for x in nums {
for c in x.to_string().chars() {
ans.push(c.to_digit(10).unwrap() asi32);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
classSolution {
separateDigits(nums: number[]):number[] {
constans: number[] = [];
for (constxofnums) {
for (constcofx.toString()) ans.push(Number(c));
}
returnans;
}
}