Skip to content

Commit

Permalink
add: 下一个更大的数值平衡数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 9, 2023
1 parent 913b7dc commit 76cc356
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,7 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8
- [找出中枢整数](src/math/find_the_pivot_integer.rs) [数学, 前缀和]

- LeetCode 2485. 找出中枢整数 <https://leetcode.cn/problems/find-the-pivot-integer>

- [下一个更大的数值平衡数](src/math/next_greater_numerically_balanced_number.rs) [数学, 回溯, 枚举]

- LeetCode 2048. 下一个更大的数值平衡数 <https://leetcode.cn/problems/next-greater-numerically-balanced-number>
5 changes: 4 additions & 1 deletion bin/cargo_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import re


def main():
test_name = None
for idx, arg in enumerate(sys.argv):
Expand All @@ -14,7 +15,8 @@ def main():

cmd = ["cargo"] + sys.argv[1:]
print("Executing command:", " ".join(cmd))
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
result = subprocess.run(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True)
print(result.stdout)

test_result_pattern = re.compile(r"test result: ok.*failed;")
Expand All @@ -28,5 +30,6 @@ def main():
else:
print("Failed test cases found. Skipping cargo xtask coverage.")


if __name__ == "__main__":
main()
18 changes: 9 additions & 9 deletions bin/ci_test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
res = re.findall(r'(\d*)\s?passed;\s?(\d*)\s?failed;', str)
conclusion = "neutral"
if "ok." in str:
conclusion = "success"
conclusion = "success"
elif "FAILED." in str:
conclusion = "failure"
conclusion = "failure"
dict = {
"conclusion": conclusion,
"stats": {
"tests": int(res[0][0]) + int(res[0][1]),
"runs": int(res[0][0]),
"failed": int(res[0][1])
}
"conclusion": conclusion,
"stats": {
"tests": int(res[0][0]) + int(res[0][1]),
"runs": int(res[0][0]),
"failed": int(res[0][1])
}
}
with open(path.abspath(path.join(path.dirname(__file__), r"testResult.json")), 'w', encoding='utf-8') as file:
file.write(json.dumps(
dict, sort_keys=False, ensure_ascii=False))
print("::set-output name=json::" + json.dumps(
dict, sort_keys=False, ensure_ascii=False))
dict, sort_keys=False, ensure_ascii=False))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub mod minimum_cost_to_connect_two_groups_of_points;
pub mod maximize_grid_happiness;
pub mod circle_and_rectangle_overlapping;
pub mod find_the_pivot_integer;
pub mod next_greater_numerically_balanced_number;
31 changes: 31 additions & 0 deletions src/math/next_greater_numerically_balanced_number.rs
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
}
}
1 change: 1 addition & 0 deletions tests/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub mod minimum_cost_to_connect_two_groups_of_points_test;
pub mod maximize_grid_happiness_test;
pub mod circle_and_rectangle_overlapping_test;
pub mod find_the_pivot_integer_test;
pub mod next_greater_numerically_balanced_number_test;
37 changes: 37 additions & 0 deletions tests/math/next_greater_numerically_balanced_number_test.rs
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);
}

0 comments on commit 76cc356

Please sign in to comment.