Skip to content

Commit cc8d96a

Browse files
committed
feat: add solutions to lc problems: No.2553~2561
1 parent e88dbef commit cc8d96a

File tree

56 files changed

+2379
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2379
-83
lines changed

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,94 @@ answer = [7,1,3,9] 。
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:模拟**
53+
54+
将数组中的每个数字进行数位分割,然后将分割后的数字依次放入答案数组中。
55+
56+
时间复杂度 $O(n \times \log_{10} M)$,空间复杂度 $O(n \times \log_{10} M)$,其中 $n$ 为数组 `nums` 的长度,而 $M$ 为数组 `nums` 中的最大值。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
<!-- 这里可写当前语言的特殊实现逻辑 -->
5763

5864
```python
59-
65+
class Solution:
66+
def separateDigits(self, nums: List[int]) -> List[int]:
67+
ans = []
68+
for x in nums:
69+
t = []
70+
while x:
71+
t.append(x % 10)
72+
x //= 10
73+
ans.extend(t[::-1])
74+
return ans
6075
```
6176

6277
### **Java**
6378

6479
<!-- 这里可写当前语言的特殊实现逻辑 -->
6580

6681
```java
67-
82+
class Solution {
83+
public int[] separateDigits(int[] nums) {
84+
List<Integer> res = new ArrayList<>();
85+
for (int x : nums) {
86+
List<Integer> t = new ArrayList<>();
87+
for (; x > 0; x /= 10) {
88+
t.add(x % 10);
89+
}
90+
Collections.reverse(t);
91+
res.addAll(t);
92+
}
93+
int[] ans = new int[res.size()];
94+
for (int i = 0; i < ans.length; ++i) {
95+
ans[i] = res.get(i);
96+
}
97+
return ans;
98+
}
99+
}
68100
```
69101

70102
### **C++**
71103

72104
```cpp
73-
105+
class Solution {
106+
public:
107+
vector<int> separateDigits(vector<int>& nums) {
108+
vector<int> ans;
109+
for (int x : nums) {
110+
vector<int> t;
111+
for (; x; x /= 10) {
112+
t.push_back(x % 10);
113+
}
114+
while (t.size()) {
115+
ans.push_back(t.back());
116+
t.pop_back();
117+
}
118+
}
119+
return ans;
120+
}
121+
};
74122
```
75123
76124
### **Go**
77125
78126
```go
79-
127+
func separateDigits(nums []int) (ans []int) {
128+
for _, x := range nums {
129+
t := []int{}
130+
for ; x > 0; x /= 10 {
131+
t = append(t, x%10)
132+
}
133+
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
134+
t[i], t[j] = t[j], t[i]
135+
}
136+
ans = append(ans, t...)
137+
}
138+
return
139+
}
80140
```
81141

82142
### **...**

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,79 @@ answer = [7,1,3,9].
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def separateDigits(self, nums: List[int]) -> List[int]:
55+
ans = []
56+
for x in nums:
57+
t = []
58+
while x:
59+
t.append(x % 10)
60+
x //= 10
61+
ans.extend(t[::-1])
62+
return ans
5463
```
5564

5665
### **Java**
5766

5867
```java
59-
68+
class Solution {
69+
public int[] separateDigits(int[] nums) {
70+
List<Integer> res = new ArrayList<>();
71+
for (int x : nums) {
72+
List<Integer> t = new ArrayList<>();
73+
for (; x > 0; x /= 10) {
74+
t.add(x % 10);
75+
}
76+
Collections.reverse(t);
77+
res.addAll(t);
78+
}
79+
int[] ans = new int[res.size()];
80+
for (int i = 0; i < ans.length; ++i) {
81+
ans[i] = res.get(i);
82+
}
83+
return ans;
84+
}
85+
}
6086
```
6187

6288
### **C++**
6389

6490
```cpp
65-
91+
class Solution {
92+
public:
93+
vector<int> separateDigits(vector<int>& nums) {
94+
vector<int> ans;
95+
for (int x : nums) {
96+
vector<int> t;
97+
for (; x; x /= 10) {
98+
t.push_back(x % 10);
99+
}
100+
while (t.size()) {
101+
ans.push_back(t.back());
102+
t.pop_back();
103+
}
104+
}
105+
return ans;
106+
}
107+
};
66108
```
67109
68110
### **Go**
69111
70112
```go
71-
113+
func separateDigits(nums []int) (ans []int) {
114+
for _, x := range nums {
115+
t := []int{}
116+
for ; x > 0; x /= 10 {
117+
t = append(t, x%10)
118+
}
119+
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
120+
t[i], t[j] = t[j], t[i]
121+
}
122+
ans = append(ans, t...)
123+
}
124+
return
125+
}
72126
```
73127

74128
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> separateDigits(vector<int>& nums) {
4+
vector<int> ans;
5+
for (int x : nums) {
6+
vector<int> t;
7+
for (; x; x /= 10) {
8+
t.push_back(x % 10);
9+
}
10+
while (t.size()) {
11+
ans.push_back(t.back());
12+
t.pop_back();
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func separateDigits(nums []int) (ans []int) {
2+
for _, x := range nums {
3+
t := []int{}
4+
for ; x > 0; x /= 10 {
5+
t = append(t, x%10)
6+
}
7+
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
8+
t[i], t[j] = t[j], t[i]
9+
}
10+
ans = append(ans, t...)
11+
}
12+
return
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] separateDigits(int[] nums) {
3+
List<Integer> res = new ArrayList<>();
4+
for (int x : nums) {
5+
List<Integer> t = new ArrayList<>();
6+
for (; x > 0; x /= 10) {
7+
t.add(x % 10);
8+
}
9+
Collections.reverse(t);
10+
res.addAll(t);
11+
}
12+
int[] ans = new int[res.size()];
13+
for (int i = 0; i < ans.length; ++i) {
14+
ans[i] = res.get(i);
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def separateDigits(self, nums: List[int]) -> List[int]:
3+
ans = []
4+
for x in nums:
5+
t = []
6+
while x:
7+
t.append(x % 10)
8+
x //= 10
9+
ans.extend(t[::-1])
10+
return ans

0 commit comments

Comments
 (0)