-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 下一个更大的数值平衡数 | ||
// https://leetcode.cn/problems/next-greater-numerically-balanced-number | ||
// INLINE ../../images/math/next_greater_numerically_balanced_number.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn is_balanced_number(x: i32) -> bool { | ||
let mut count = vec![0; 10]; | ||
let mut x = x; | ||
while x > 0 { | ||
count[(x % 10) as usize] += 1; | ||
x /= 10; | ||
} | ||
for d in 0..10 { | ||
if count[d] > 0 && count[d] != d { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
pub fn next_beautiful_number(n: i32) -> i32 { | ||
for i in n + 1..=1224444 { | ||
if Self::is_balanced_number(i) { | ||
return i; | ||
} | ||
} | ||
-1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/math/next_greater_numerically_balanced_number_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use rust_practice::math::next_greater_numerically_balanced_number::Solution; | ||
|
||
#[test] | ||
fn next_beautiful_number() { | ||
// 示例 1: | ||
// 输入:n = 1 | ||
// 输出:22 | ||
// 解释: | ||
// 22 是一个数值平衡数,因为: | ||
// - 数字 2 出现 2 次 | ||
// 这也是严格大于 1 的最小数值平衡数。 | ||
let n = 1; | ||
assert_eq!(Solution::next_beautiful_number(n), 22); | ||
|
||
// 示例 2: | ||
// 输入:n = 1000 | ||
// 输出:1333 | ||
// 解释: | ||
// 1333 是一个数值平衡数,因为: | ||
// - 数字 1 出现 1 次。 | ||
// - 数字 3 出现 3 次。 | ||
// 这也是严格大于 1000 的最小数值平衡数。 | ||
// 注意,1022 不能作为本输入的答案,因为数字 0 的出现次数超过了 0 。 | ||
let n = 1000; | ||
assert_eq!(Solution::next_beautiful_number(n), 1333); | ||
|
||
// 示例 3: | ||
// 输入:n = 3000 | ||
// 输出:3133 | ||
// 解释: | ||
// 3133 是一个数值平衡数,因为: | ||
// - 数字 1 出现 1 次。 | ||
// - 数字 3 出现 3 次。 | ||
// 这也是严格大于 3000 的最小数值平衡数。 | ||
let n = 3000; | ||
assert_eq!(Solution::next_beautiful_number(n), 3133); | ||
} |