From 2efcf2f9bb0b571c484389e04511d322f4372891 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 24 Oct 2025 19:15:56 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2048 --- .../README.md | 38 +++++++++++++++-- .../README_EN.md | 42 ++++++++++++++++--- .../Solution.rs | 27 ++++++++++++ 3 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.rs diff --git a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md index 71bb9c8ccc49a..5fed7d7ed8720 100644 --- a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md +++ b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md @@ -35,7 +35,7 @@ tags: 输出:22 解释: 22 是一个数值平衡数,因为: -- 数字 2 出现 2 次 +- 数字 2 出现 2 次 这也是严格大于 1 的最小数值平衡数。 @@ -47,7 +47,7 @@ tags: 解释: 1333 是一个数值平衡数,因为: - 数字 1 出现 1 次。 -- 数字 3 出现 3 次。 +- 数字 3 出现 3 次。 这也是严格大于 1000 的最小数值平衡数。 注意,1022 不能作为本输入的答案,因为数字 0 的出现次数超过了 0 。 @@ -59,7 +59,7 @@ tags: 解释: 3133 是一个数值平衡数,因为: - 数字 1 出现 1 次。 -- 数字 3 出现 3 次。 +- 数字 3 出现 3 次。 这也是严格大于 3000 的最小数值平衡数。 @@ -197,6 +197,38 @@ function nextBeautifulNumber(n: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn next_beautiful_number(n: i32) -> i32 { + let mut x = n + 1; + loop { + let mut cnt = [0; 10]; + let mut y = x; + while y > 0 { + cnt[(y % 10) as usize] += 1; + y /= 10; + } + let mut ok = true; + let mut y2 = x; + while y2 > 0 { + let d = (y2 % 10) as usize; + if d != cnt[d] { + ok = false; + break; + } + y2 /= 10; + } + if ok { + return x; + } + x += 1; + } + } +} +``` + diff --git a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md index 800daeb6a071e..326ca68dfbe67 100644 --- a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md +++ b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/README_EN.md @@ -32,9 +32,9 @@ tags:
 Input: n = 1
 Output: 22
-Explanation: 
+Explanation:
 22 is numerically balanced since:
-- The digit 2 occurs 2 times. 
+- The digit 2 occurs 2 times.
 It is also the smallest numerically balanced number strictly greater than 1.
 
@@ -43,10 +43,10 @@ It is also the smallest numerically balanced number strictly greater than 1.
 Input: n = 1000
 Output: 1333
-Explanation: 
+Explanation:
 1333 is numerically balanced since:
 - The digit 1 occurs 1 time.
-- The digit 3 occurs 3 times. 
+- The digit 3 occurs 3 times.
 It is also the smallest numerically balanced number strictly greater than 1000.
 Note that 1022 cannot be the answer because 0 appeared more than 0 times.
 
@@ -56,7 +56,7 @@ Note that 1022 cannot be the answer because 0 appeared more than 0 times.
 Input: n = 3000
 Output: 3133
-Explanation: 
+Explanation:
 3133 is numerically balanced since:
 - The digit 1 occurs 1 time.
 - The digit 3 occurs 3 times.
@@ -196,6 +196,38 @@ function nextBeautifulNumber(n: number): number {
 }
 ```
 
+#### Rust
+
+```rust
+impl Solution {
+    pub fn next_beautiful_number(n: i32) -> i32 {
+        let mut x = n + 1;
+        loop {
+            let mut cnt = [0; 10];
+            let mut y = x;
+            while y > 0 {
+                cnt[(y % 10) as usize] += 1;
+                y /= 10;
+            }
+            let mut ok = true;
+            let mut y2 = x;
+            while y2 > 0 {
+                let d = (y2 % 10) as usize;
+                if d != cnt[d] {
+                    ok = false;
+                    break;
+                }
+                y2 /= 10;
+            }
+            if ok {
+                return x;
+            }
+            x += 1;
+        }
+    }
+}
+```
+
 
 
 
diff --git a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.rs b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.rs
new file mode 100644
index 0000000000000..51e57e71d7c44
--- /dev/null
+++ b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.rs	
@@ -0,0 +1,27 @@
+impl Solution {
+    pub fn next_beautiful_number(n: i32) -> i32 {
+        let mut x = n + 1;
+        loop {
+            let mut cnt = [0; 10];
+            let mut y = x;
+            while y > 0 {
+                cnt[(y % 10) as usize] += 1;
+                y /= 10;
+            }
+            let mut ok = true;
+            let mut y2 = x;
+            while y2 > 0 {
+                let d = (y2 % 10) as usize;
+                if d != cnt[d] {
+                    ok = false;
+                    break;
+                }
+                y2 /= 10;
+            }
+            if ok {
+                return x;
+            }
+            x += 1;
+        }
+    }
+}