5656
5757<!-- solution:start -->
5858
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)$。
6068
6169<!-- tabs:start -->
6270
@@ -65,68 +73,67 @@ tags:
6573``` python
6674class Solution :
6775 def smallestNumber (self , num : int ) -> int :
68- if num == 0 :
69- return 0
70- cnt = [0 ] * 10
7176 neg = num < 0
7277 num = abs (num)
78+ cnt = [0 ] * 10
7379 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
7783 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
8289 if cnt[0 ]:
8390 for i in range (1 , 10 ):
8491 if cnt[i]:
85- ans += str (i)
92+ ans = i
8693 cnt[i] -= 1
8794 break
8895 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
92100```
93101
94102#### Java
95103
96104``` java
97105class Solution {
98106 public long smallestNumber (long num ) {
99- if (num == 0 ) {
100- return 0 ;
101- }
102- int [] cnt = new int [10 ];
103107 boolean neg = num < 0 ;
104108 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 )];
107112 num /= 10 ;
108113 }
109114 long ans = 0 ;
110115 if (neg) {
111116 for (int i = 9 ; i >= 0 ; -- i) {
112- while (cnt[i]-- > 0 ) {
117+ while (cnt[i] > 0 ) {
113118 ans = ans * 10 + i;
119+ -- cnt[i];
114120 }
115121 }
116122 return - ans;
117123 }
118124 if (cnt[0 ] > 0 ) {
119125 for (int i = 1 ; i < 10 ; ++ i) {
120126 if (cnt[i] > 0 ) {
121- ans = ans * 10 + i ;
122- cnt[i] -- ;
127+ -- cnt[i] ;
128+ ans = i ;
123129 break ;
124130 }
125131 }
126132 }
127133 for (int i = 0 ; i < 10 ; ++ i) {
128- while (cnt[i]-- > 0 ) {
134+ while (cnt[i] > 0 ) {
129135 ans = ans * 10 + i;
136+ -- cnt[i];
130137 }
131138 }
132139 return ans;
@@ -140,31 +147,38 @@ class Solution {
140147class Solution {
141148public:
142149 long long smallestNumber(long long num) {
143- if (num == 0) return 0;
144- vector<int > cnt(10);
145150 bool neg = num < 0;
146151 num = abs(num);
147- while (num) {
148- cnt[ num % 10] ++;
152+ int cnt[ 10] {};
153+ while (num > 0) {
154+ ++cnt[ num % 10] ;
149155 num /= 10;
150156 }
151157 long long ans = 0;
152158 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+ }
155165 return -ans;
156166 }
157167 if (cnt[ 0] ) {
158168 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 ;
162172 break;
163173 }
164174 }
165175 }
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+ }
168182 return ans;
169183 }
170184};
@@ -173,46 +187,188 @@ public:
173187#### Go
174188
175189```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) {
181191 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 {
186196 cnt[num%10]++
187197 num /= 10
188198 }
189- ans := 0
199+
190200 if neg {
191201 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]--
194205 }
195206 }
196- return -int64( ans)
207+ return -ans
197208 }
209+
198210 if cnt[0] > 0 {
199211 for i := 1; i < 10; i++ {
200212 if cnt[i] > 0 {
201- ans = ans*10 + i
202213 cnt[i]--
214+ ans = int64(i)
203215 break
204216 }
205217 }
206218 }
219+
207220 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]--
210224 }
211225 }
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 ;
213273}
214274```
215275
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+
216372<!-- tabs: end -->
217373
218374<!-- solution: end -->
0 commit comments