Skip to content

Commit 1ffa85e

Browse files
committed
feat: add solutions to lc problem: No.2553
No.2553.Separate the Digits in an Array
1 parent eb1814a commit 1ffa85e

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

solution/2500-2599/2553.Separate the Digits in an Array/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,71 @@ func separateDigits(nums []int) (ans []int) {
139139
}
140140
```
141141

142+
### **TypeScript**
143+
144+
```ts
145+
function separateDigits(nums: number[]): number[] {
146+
const ans: number[] = [];
147+
for (let num of nums) {
148+
const t: number[] = [];
149+
while (num) {
150+
t.push(num % 10);
151+
num = Math.floor(num / 10);
152+
}
153+
ans.push(...t.reverse());
154+
}
155+
return ans;
156+
}
157+
```
158+
159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
pub fn separate_digits(nums: Vec<i32>) -> Vec<i32> {
164+
let mut ans = Vec::new();
165+
for &num in nums.iter() {
166+
let mut num = num;
167+
let mut t = Vec::new();
168+
while num != 0 {
169+
t.push(num % 10);
170+
num /= 10;
171+
}
172+
t.into_iter().rev().for_each(|v| ans.push(v));
173+
}
174+
ans
175+
}
176+
}
177+
```
178+
179+
### **C**
180+
181+
```c
182+
/**
183+
* Note: The returned array must be malloced, assume caller calls free().
184+
*/
185+
int *separateDigits(int *nums, int numsSize, int *returnSize) {
186+
int n = 0;
187+
for (int i = 0; i < numsSize; i++) {
188+
int t = nums[i];
189+
while (t != 0) {
190+
t /= 10;
191+
n++;
192+
}
193+
}
194+
int *ans = malloc(sizeof(int) * n);
195+
for (int i = numsSize - 1, j = n - 1; i >= 0; i--) {
196+
int t = nums[i];
197+
while (t != 0) {
198+
ans[j--] = t % 10;
199+
t /= 10;
200+
}
201+
}
202+
*returnSize = n;
203+
return ans;
204+
}
205+
```
206+
142207
### **...**
143208
144209
```

solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,71 @@ func separateDigits(nums []int) (ans []int) {
125125
}
126126
```
127127

128+
### **TypeScript**
129+
130+
```ts
131+
function separateDigits(nums: number[]): number[] {
132+
const ans: number[] = [];
133+
for (let num of nums) {
134+
const t: number[] = [];
135+
while (num) {
136+
t.push(num % 10);
137+
num = Math.floor(num / 10);
138+
}
139+
ans.push(...t.reverse());
140+
}
141+
return ans;
142+
}
143+
```
144+
145+
### **Rust**
146+
147+
```rust
148+
impl Solution {
149+
pub fn separate_digits(nums: Vec<i32>) -> Vec<i32> {
150+
let mut ans = Vec::new();
151+
for &num in nums.iter() {
152+
let mut num = num;
153+
let mut t = Vec::new();
154+
while num != 0 {
155+
t.push(num % 10);
156+
num /= 10;
157+
}
158+
t.into_iter().rev().for_each(|v| ans.push(v));
159+
}
160+
ans
161+
}
162+
}
163+
```
164+
165+
### **C**
166+
167+
```c
168+
/**
169+
* Note: The returned array must be malloced, assume caller calls free().
170+
*/
171+
int *separateDigits(int *nums, int numsSize, int *returnSize) {
172+
int n = 0;
173+
for (int i = 0; i < numsSize; i++) {
174+
int t = nums[i];
175+
while (t != 0) {
176+
t /= 10;
177+
n++;
178+
}
179+
}
180+
int *ans = malloc(sizeof(int) * n);
181+
for (int i = numsSize - 1, j = n - 1; i >= 0; i--) {
182+
int t = nums[i];
183+
while (t != 0) {
184+
ans[j--] = t % 10;
185+
t /= 10;
186+
}
187+
}
188+
*returnSize = n;
189+
return ans;
190+
}
191+
```
192+
128193
### **...**
129194
130195
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *separateDigits(int *nums, int numsSize, int *returnSize) {
5+
int n = 0;
6+
for (int i = 0; i < numsSize; i++) {
7+
int t = nums[i];
8+
while (t != 0) {
9+
t /= 10;
10+
n++;
11+
}
12+
}
13+
int *ans = malloc(sizeof(int) * n);
14+
for (int i = numsSize - 1, j = n - 1; i >= 0; i--) {
15+
int t = nums[i];
16+
while (t != 0) {
17+
ans[j--] = t % 10;
18+
t /= 10;
19+
}
20+
}
21+
*returnSize = n;
22+
return ans;
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn separate_digits(nums: Vec<i32>) -> Vec<i32> {
3+
let mut ans = Vec::new();
4+
for &num in nums.iter() {
5+
let mut num = num;
6+
let mut t = Vec::new();
7+
while num != 0 {
8+
t.push(num % 10);
9+
num /= 10;
10+
}
11+
t.into_iter().rev().for_each(|v| ans.push(v));
12+
}
13+
ans
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function separateDigits(nums: number[]): number[] {
2+
const ans: number[] = [];
3+
for (let num of nums) {
4+
const t: number[] = [];
5+
while (num) {
6+
t.push(num % 10);
7+
num = Math.floor(num / 10);
8+
}
9+
ans.push(...t.reverse());
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)