Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6445c81

Browse files
feat: add rust solution to lc problem: No.2048 (#4796)
1 parent 7ffcdf6 commit 6445c81

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

‎solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tags:
3535
<strong>输出:</strong>22
3636
<strong>解释:</strong>
3737
22 是一个数值平衡数,因为:
38-
- 数字 2 出现 2 次
38+
- 数字 2 出现 2 次
3939
这也是严格大于 1 的最小数值平衡数。
4040
</pre>
4141

@@ -47,7 +47,7 @@ tags:
4747
<strong>解释:</strong>
4848
1333 是一个数值平衡数,因为:
4949
- 数字 1 出现 1 次。
50-
- 数字 3 出现 3 次。
50+
- 数字 3 出现 3 次。
5151
这也是严格大于 1000 的最小数值平衡数。
5252
注意,1022 不能作为本输入的答案,因为数字 0 的出现次数超过了 0 。</pre>
5353

@@ -59,7 +59,7 @@ tags:
5959
<strong>解释:</strong>
6060
3133 是一个数值平衡数,因为:
6161
- 数字 1 出现 1 次。
62-
- 数字 3 出现 3 次。
62+
- 数字 3 出现 3 次。
6363
这也是严格大于 3000 的最小数值平衡数。
6464
</pre>
6565

@@ -197,6 +197,38 @@ function nextBeautifulNumber(n: number): number {
197197
}
198198
```
199199

200+
#### Rust
201+
202+
```rust
203+
impl Solution {
204+
pub fn next_beautiful_number(n: i32) -> i32 {
205+
let mut x = n + 1;
206+
loop {
207+
let mut cnt = [0; 10];
208+
let mut y = x;
209+
while y > 0 {
210+
cnt[(y % 10) as usize] += 1;
211+
y /= 10;
212+
}
213+
let mut ok = true;
214+
let mut y2 = x;
215+
while y2 > 0 {
216+
let d = (y2 % 10) as usize;
217+
if d != cnt[d] {
218+
ok = false;
219+
break;
220+
}
221+
y2 /= 10;
222+
}
223+
if ok {
224+
return x;
225+
}
226+
x += 1;
227+
}
228+
}
229+
}
230+
```
231+
200232
<!-- tabs:end -->
201233

202234
<!-- solution:end -->

‎solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ tags:
3232
<pre>
3333
<strong>Input:</strong> n = 1
3434
<strong>Output:</strong> 22
35-
<strong>Explanation:</strong>
35+
<strong>Explanation:</strong>
3636
22 is numerically balanced since:
37-
- The digit 2 occurs 2 times.
37+
- The digit 2 occurs 2 times.
3838
It is also the smallest numerically balanced number strictly greater than 1.
3939
</pre>
4040

@@ -43,10 +43,10 @@ It is also the smallest numerically balanced number strictly greater than 1.
4343
<pre>
4444
<strong>Input:</strong> n = 1000
4545
<strong>Output:</strong> 1333
46-
<strong>Explanation:</strong>
46+
<strong>Explanation:</strong>
4747
1333 is numerically balanced since:
4848
- The digit 1 occurs 1 time.
49-
- The digit 3 occurs 3 times.
49+
- The digit 3 occurs 3 times.
5050
It is also the smallest numerically balanced number strictly greater than 1000.
5151
Note that 1022 cannot be the answer because 0 appeared more than 0 times.
5252
</pre>
@@ -56,7 +56,7 @@ Note that 1022 cannot be the answer because 0 appeared more than 0 times.
5656
<pre>
5757
<strong>Input:</strong> n = 3000
5858
<strong>Output:</strong> 3133
59-
<strong>Explanation:</strong>
59+
<strong>Explanation:</strong>
6060
3133 is numerically balanced since:
6161
- The digit 1 occurs 1 time.
6262
- The digit 3 occurs 3 times.
@@ -196,6 +196,38 @@ function nextBeautifulNumber(n: number): number {
196196
}
197197
```
198198

199+
#### Rust
200+
201+
```rust
202+
impl Solution {
203+
pub fn next_beautiful_number(n: i32) -> i32 {
204+
let mut x = n + 1;
205+
loop {
206+
let mut cnt = [0; 10];
207+
let mut y = x;
208+
while y > 0 {
209+
cnt[(y % 10) as usize] += 1;
210+
y /= 10;
211+
}
212+
let mut ok = true;
213+
let mut y2 = x;
214+
while y2 > 0 {
215+
let d = (y2 % 10) as usize;
216+
if d != cnt[d] {
217+
ok = false;
218+
break;
219+
}
220+
y2 /= 10;
221+
}
222+
if ok {
223+
return x;
224+
}
225+
x += 1;
226+
}
227+
}
228+
}
229+
```
230+
199231
<!-- tabs:end -->
200232

201233
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn next_beautiful_number(n: i32) -> i32 {
3+
let mut x = n + 1;
4+
loop {
5+
let mut cnt = [0; 10];
6+
let mut y = x;
7+
while y > 0 {
8+
cnt[(y % 10) as usize] += 1;
9+
y /= 10;
10+
}
11+
let mut ok = true;
12+
let mut y2 = x;
13+
while y2 > 0 {
14+
let d = (y2 % 10) as usize;
15+
if d != cnt[d] {
16+
ok = false;
17+
break;
18+
}
19+
y2 /= 10;
20+
}
21+
if ok {
22+
return x;
23+
}
24+
x += 1;
25+
}
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /