diff --git a/solution/2800-2899/2843.Count Symmetric Integers/README.md b/solution/2800-2899/2843.Count Symmetric Integers/README.md index d494fdf7e0db9..d8de9515b250e 100644 --- a/solution/2800-2899/2843.Count Symmetric Integers/README.md +++ b/solution/2800-2899/2843.Count Symmetric Integers/README.md @@ -191,6 +191,68 @@ function countSymmetricIntegers(low: number, high: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_symmetric_integers(low: i32, high: i32) -> i32 { + let mut ans = 0; + for x in low..=high { + ans += Self::f(x); + } + ans + } + + fn f(x: i32) -> i32 { + let s = x.to_string(); + let n = s.len(); + if n % 2 == 1 { + return 0; + } + let bytes = s.as_bytes(); + let mut a = 0; + let mut b = 0; + for i in 0..n / 2 { + a += (bytes[i] - b'0') as i32; + } + for i in n / 2..n { + b += (bytes[i] - b'0') as i32; + } + if a == b { 1 } else { 0 } + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountSymmetricIntegers(int low, int high) { + int ans = 0; + for (int x = low; x <= high; ++x) { + ans += f(x); + } + return ans; + } + + private int f(int x) { + string s = x.ToString(); + int n = s.Length; + if (n % 2 == 1) { + return 0; + } + int a = 0, b = 0; + for (int i = 0; i < n / 2; ++i) { + a += s[i] - '0'; + } + for (int i = n / 2; i < n; ++i) { + b += s[i] - '0'; + } + return a == b ? 1 : 0; + } +} +``` + diff --git a/solution/2800-2899/2843.Count Symmetric Integers/README_EN.md b/solution/2800-2899/2843.Count Symmetric Integers/README_EN.md index e7b42f59c891c..789aa729b0d17 100644 --- a/solution/2800-2899/2843.Count Symmetric Integers/README_EN.md +++ b/solution/2800-2899/2843.Count Symmetric Integers/README_EN.md @@ -189,6 +189,68 @@ function countSymmetricIntegers(low: number, high: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_symmetric_integers(low: i32, high: i32) -> i32 { + let mut ans = 0; + for x in low..=high { + ans += Self::f(x); + } + ans + } + + fn f(x: i32) -> i32 { + let s = x.to_string(); + let n = s.len(); + if n % 2 == 1 { + return 0; + } + let bytes = s.as_bytes(); + let mut a = 0; + let mut b = 0; + for i in 0..n / 2 { + a += (bytes[i] - b'0') as i32; + } + for i in n / 2..n { + b += (bytes[i] - b'0') as i32; + } + if a == b { 1 } else { 0 } + } +} +``` + +#### C# + +```cs +public class Solution { + public int CountSymmetricIntegers(int low, int high) { + int ans = 0; + for (int x = low; x <= high; ++x) { + ans += f(x); + } + return ans; + } + + private int f(int x) { + string s = x.ToString(); + int n = s.Length; + if (n % 2 == 1) { + return 0; + } + int a = 0, b = 0; + for (int i = 0; i < n / 2; ++i) { + a += s[i] - '0'; + } + for (int i = n / 2; i < n; ++i) { + b += s[i] - '0'; + } + return a == b ? 1 : 0; + } +} +``` + diff --git a/solution/2800-2899/2843.Count Symmetric Integers/Solution.cs b/solution/2800-2899/2843.Count Symmetric Integers/Solution.cs new file mode 100644 index 0000000000000..25c0d0cafd614 --- /dev/null +++ b/solution/2800-2899/2843.Count Symmetric Integers/Solution.cs @@ -0,0 +1,25 @@ +public class Solution { + public int CountSymmetricIntegers(int low, int high) { + int ans = 0; + for (int x = low; x <= high; ++x) { + ans += f(x); + } + return ans; + } + + private int f(int x) { + string s = x.ToString(); + int n = s.Length; + if (n % 2 == 1) { + return 0; + } + int a = 0, b = 0; + for (int i = 0; i < n / 2; ++i) { + a += s[i] - '0'; + } + for (int i = n / 2; i < n; ++i) { + b += s[i] - '0'; + } + return a == b ? 1 : 0; + } +} diff --git a/solution/2800-2899/2843.Count Symmetric Integers/Solution.rs b/solution/2800-2899/2843.Count Symmetric Integers/Solution.rs new file mode 100644 index 0000000000000..79c4613f7badc --- /dev/null +++ b/solution/2800-2899/2843.Count Symmetric Integers/Solution.rs @@ -0,0 +1,31 @@ +impl Solution { + pub fn count_symmetric_integers(low: i32, high: i32) -> i32 { + let mut ans = 0; + for x in low..=high { + ans += Self::f(x); + } + ans + } + + fn f(x: i32) -> i32 { + let s = x.to_string(); + let n = s.len(); + if n % 2 == 1 { + return 0; + } + let bytes = s.as_bytes(); + let mut a = 0; + let mut b = 0; + for i in 0..n / 2 { + a += (bytes[i] - b'0') as i32; + } + for i in n / 2..n { + b += (bytes[i] - b'0') as i32; + } + if a == b { + 1 + } else { + 0 + } + } +}