56
56
57
57
<!-- solution:start -->
58
58
59
- ### 方法一
59
+ ### 方法一:计数
60
+
61
+ 我们首先用一个数组 $\textit{cnt}$ 记录 $\textit{num}$ 中每个数字的出现次数。
62
+
63
+ 如果 $\textit{num}$ 是负数,那么数字应该按照从大到小的顺序排列,因此我们从 $9$ 到 $0$ 遍历 $\textit{cnt}$,将数字按照出现次数从大到小的顺序排列。
64
+
65
+ 如果是正数,我们首先找到第一个非 $0$ 的数字,将其放在第一位,然后按照从小到大的顺序排列剩余的数字。
66
+
67
+ 时间复杂度 $O(\log n)$,其中 $n$ 为数字 $\textit{num}$ 的大小。空间复杂度 $O(1)$。
60
68
61
69
<!-- tabs:start -->
62
70
@@ -65,68 +73,67 @@ tags:
65
73
``` python
66
74
class Solution :
67
75
def smallestNumber (self , num : int ) -> int :
68
- if num == 0 :
69
- return 0
70
- cnt = [0 ] * 10
71
76
neg = num < 0
72
77
num = abs (num)
78
+ cnt = [0 ] * 10
73
79
while num:
74
- num, v = divmod (num, 10 )
75
- cnt[v] += 1
76
- ans = " "
80
+ cnt[ num % 10 ] += 1
81
+ num //= 10
82
+ ans = 0
77
83
if neg:
78
- for i in range (9 , - 1 , - 1 ):
79
- if cnt[i]:
80
- ans += str (i) * cnt[i]
81
- return - int (ans)
84
+ for i in reversed (range (10 )):
85
+ for _ in range (cnt[i]):
86
+ ans *= 10
87
+ ans += i
88
+ return - ans
82
89
if cnt[0 ]:
83
90
for i in range (1 , 10 ):
84
91
if cnt[i]:
85
- ans += str (i)
92
+ ans = i
86
93
cnt[i] -= 1
87
94
break
88
95
for i in range (10 ):
89
- if cnt[i]:
90
- ans += str (i) * cnt[i]
91
- return int (ans)
96
+ for _ in range (cnt[i]):
97
+ ans *= 10
98
+ ans += i
99
+ return ans
92
100
```
93
101
94
102
#### Java
95
103
96
104
``` java
97
105
class Solution {
98
106
public long smallestNumber (long num ) {
99
- if (num == 0 ) {
100
- return 0 ;
101
- }
102
- int [] cnt = new int [10 ];
103
107
boolean neg = num < 0 ;
104
108
num = Math . abs(num);
105
- while (num != 0 ) {
106
- cnt[(int ) (num % 10 )]++ ;
109
+ int [] cnt = new int [10 ];
110
+ while (num > 0 ) {
111
+ ++ cnt[(int ) (num % 10 )];
107
112
num /= 10 ;
108
113
}
109
114
long ans = 0 ;
110
115
if (neg) {
111
116
for (int i = 9 ; i >= 0 ; -- i) {
112
- while (cnt[i]-- > 0 ) {
117
+ while (cnt[i] > 0 ) {
113
118
ans = ans * 10 + i;
119
+ -- cnt[i];
114
120
}
115
121
}
116
122
return - ans;
117
123
}
118
124
if (cnt[0 ] > 0 ) {
119
125
for (int i = 1 ; i < 10 ; ++ i) {
120
126
if (cnt[i] > 0 ) {
121
- ans = ans * 10 + i ;
122
- cnt[i] -- ;
127
+ -- cnt[i] ;
128
+ ans = i ;
123
129
break ;
124
130
}
125
131
}
126
132
}
127
133
for (int i = 0 ; i < 10 ; ++ i) {
128
- while (cnt[i]-- > 0 ) {
134
+ while (cnt[i] > 0 ) {
129
135
ans = ans * 10 + i;
136
+ -- cnt[i];
130
137
}
131
138
}
132
139
return ans;
@@ -140,31 +147,38 @@ class Solution {
140
147
class Solution {
141
148
public:
142
149
long long smallestNumber(long long num) {
143
- if (num == 0) return 0;
144
- vector<int > cnt(10);
145
150
bool neg = num < 0;
146
151
num = abs(num);
147
- while (num) {
148
- cnt[ num % 10] ++;
152
+ int cnt[ 10] {};
153
+ while (num > 0) {
154
+ ++cnt[ num % 10] ;
149
155
num /= 10;
150
156
}
151
157
long long ans = 0;
152
158
if (neg) {
153
- for (int i = 9; i >= 0; --i)
154
- while (cnt[ i] --) ans = ans * 10 + i;
159
+ for (int i = 9; i >= 0; --i) {
160
+ while (cnt[ i] > 0) {
161
+ ans = ans * 10 + i;
162
+ --cnt[ i] ;
163
+ }
164
+ }
155
165
return -ans;
156
166
}
157
167
if (cnt[ 0] ) {
158
168
for (int i = 1; i < 10; ++i) {
159
- if (cnt[ i] ) {
160
- ans = ans * 10 + i ;
161
- cnt [ i ] -- ;
169
+ if (cnt[ i] > 0 ) {
170
+ --cnt [ i ] ;
171
+ ans = i ;
162
172
break;
163
173
}
164
174
}
165
175
}
166
- for (int i = 0; i < 10; ++i)
167
- while (cnt[ i] --) ans = ans * 10 + i;
176
+ for (int i = 0; i < 10; ++i) {
177
+ while (cnt[ i] > 0) {
178
+ ans = ans * 10 + i;
179
+ --cnt[ i] ;
180
+ }
181
+ }
168
182
return ans;
169
183
}
170
184
};
@@ -173,46 +187,188 @@ public:
173
187
#### Go
174
188
175
189
```go
176
- func smallestNumber(num int64) int64 {
177
- if num == 0 {
178
- return 0
179
- }
180
- cnt := make([]int, 10)
190
+ func smallestNumber(num int64) (ans int64) {
181
191
neg := num < 0
182
- if neg {
183
- num = -num
184
- }
185
- for num != 0 {
192
+ num = max(num, -num)
193
+ cnt := make([]int, 10)
194
+
195
+ for num > 0 {
186
196
cnt[num%10]++
187
197
num /= 10
188
198
}
189
- ans := 0
199
+
190
200
if neg {
191
201
for i := 9; i >= 0; i-- {
192
- for j := 0; j < cnt[i]; j++ {
193
- ans = ans*10 + i
202
+ for cnt[i] > 0 {
203
+ ans = ans*10 + int64(i)
204
+ cnt[i]--
194
205
}
195
206
}
196
- return -int64( ans)
207
+ return -ans
197
208
}
209
+
198
210
if cnt[0] > 0 {
199
211
for i := 1; i < 10; i++ {
200
212
if cnt[i] > 0 {
201
- ans = ans*10 + i
202
213
cnt[i]--
214
+ ans = int64(i)
203
215
break
204
216
}
205
217
}
206
218
}
219
+
207
220
for i := 0; i < 10; i++ {
208
- for j := 0; j < cnt[i]; j++ {
209
- ans = ans*10 + i
221
+ for cnt[i] > 0 {
222
+ ans = ans*10 + int64(i)
223
+ cnt[i]--
210
224
}
211
225
}
212
- return int64(ans)
226
+
227
+ return ans
228
+ }
229
+ ```
230
+
231
+ #### TypeScript
232
+
233
+ ``` ts
234
+ function smallestNumber(num : number ): number {
235
+ const neg = num < 0 ;
236
+ num = Math .abs (num );
237
+ const cnt = Array (10 ).fill (0 );
238
+
239
+ while (num > 0 ) {
240
+ cnt [num % 10 ]++ ;
241
+ num = Math .floor (num / 10 );
242
+ }
243
+
244
+ let ans = 0 ;
245
+ if (neg ) {
246
+ for (let i = 9 ; i >= 0 ; i -- ) {
247
+ while (cnt [i ] > 0 ) {
248
+ ans = ans * 10 + i ;
249
+ cnt [i ]-- ;
250
+ }
251
+ }
252
+ return - ans ;
253
+ }
254
+
255
+ if (cnt [0 ] > 0 ) {
256
+ for (let i = 1 ; i < 10 ; i ++ ) {
257
+ if (cnt [i ] > 0 ) {
258
+ cnt [i ]-- ;
259
+ ans = i ;
260
+ break ;
261
+ }
262
+ }
263
+ }
264
+
265
+ for (let i = 0 ; i < 10 ; i ++ ) {
266
+ while (cnt [i ] > 0 ) {
267
+ ans = ans * 10 + i ;
268
+ cnt [i ]-- ;
269
+ }
270
+ }
271
+
272
+ return ans ;
213
273
}
214
274
```
215
275
276
+ #### Rust
277
+
278
+ ``` rust
279
+ impl Solution {
280
+ pub fn smallest_number (num : i64 ) -> i64 {
281
+ let mut neg = num < 0 ;
282
+ let mut num = num . abs ();
283
+ let mut cnt = vec! [0 ; 10 ];
284
+
285
+ while num > 0 {
286
+ cnt [(num % 10 ) as usize ] += 1 ;
287
+ num /= 10 ;
288
+ }
289
+
290
+ let mut ans = 0 ;
291
+ if neg {
292
+ for i in (0 .. 10 ). rev () {
293
+ while cnt [i ] > 0 {
294
+ ans = ans * 10 + i as i64 ;
295
+ cnt [i ] -= 1 ;
296
+ }
297
+ }
298
+ return - ans ;
299
+ }
300
+
301
+ if cnt [0 ] > 0 {
302
+ for i in 1 .. 10 {
303
+ if cnt [i ] > 0 {
304
+ cnt [i ] -= 1 ;
305
+ ans = i as i64 ;
306
+ break ;
307
+ }
308
+ }
309
+ }
310
+
311
+ for i in 0 .. 10 {
312
+ while cnt [i ] > 0 {
313
+ ans = ans * 10 + i as i64 ;
314
+ cnt [i ] -= 1 ;
315
+ }
316
+ }
317
+
318
+ ans
319
+ }
320
+ }
321
+ ```
322
+
323
+ #### JavaScript
324
+
325
+ ``` js
326
+ /**
327
+ * @param {number} num
328
+ * @return {number}
329
+ */
330
+ var smallestNumber = function (num ) {
331
+ const neg = num < 0 ;
332
+ num = Math .abs (num);
333
+ const cnt = Array (10 ).fill (0 );
334
+
335
+ while (num > 0 ) {
336
+ cnt[num % 10 ]++ ;
337
+ num = Math .floor (num / 10 );
338
+ }
339
+
340
+ let ans = 0 ;
341
+ if (neg) {
342
+ for (let i = 9 ; i >= 0 ; i-- ) {
343
+ while (cnt[i] > 0 ) {
344
+ ans = ans * 10 + i;
345
+ cnt[i]-- ;
346
+ }
347
+ }
348
+ return - ans;
349
+ }
350
+
351
+ if (cnt[0 ] > 0 ) {
352
+ for (let i = 1 ; i < 10 ; i++ ) {
353
+ if (cnt[i] > 0 ) {
354
+ cnt[i]-- ;
355
+ ans = i;
356
+ break ;
357
+ }
358
+ }
359
+ }
360
+
361
+ for (let i = 0 ; i < 10 ; i++ ) {
362
+ while (cnt[i] > 0 ) {
363
+ ans = ans * 10 + i;
364
+ cnt[i]-- ;
365
+ }
366
+ }
367
+
368
+ return ans;
369
+ };
370
+ ```
371
+
216
372
<!-- tabs: end -->
217
373
218
374
<!-- solution: end -->
0 commit comments