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 1371b77

Browse files
feat: add solutions to lc problem: No.2843 (doocs#4345)
No.2843.Count Symmetric Integers
1 parent 83a3bde commit 1371b77

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

‎solution/2800-2899/2843.Count Symmetric Integers/README.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,68 @@ function countSymmetricIntegers(low: number, high: number): number {
191191
}
192192
```
193193

194+
#### Rust
195+
196+
```rust
197+
impl Solution {
198+
pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
199+
let mut ans = 0;
200+
for x in low..=high {
201+
ans += Self::f(x);
202+
}
203+
ans
204+
}
205+
206+
fn f(x: i32) -> i32 {
207+
let s = x.to_string();
208+
let n = s.len();
209+
if n % 2 == 1 {
210+
return 0;
211+
}
212+
let bytes = s.as_bytes();
213+
let mut a = 0;
214+
let mut b = 0;
215+
for i in 0..n / 2 {
216+
a += (bytes[i] - b'0') as i32;
217+
}
218+
for i in n / 2..n {
219+
b += (bytes[i] - b'0') as i32;
220+
}
221+
if a == b { 1 } else { 0 }
222+
}
223+
}
224+
```
225+
226+
#### C#
227+
228+
```cs
229+
public class Solution {
230+
public int CountSymmetricIntegers(int low, int high) {
231+
int ans = 0;
232+
for (int x = low; x <= high; ++x) {
233+
ans += f(x);
234+
}
235+
return ans;
236+
}
237+
238+
private int f(int x) {
239+
string s = x.ToString();
240+
int n = s.Length;
241+
if (n % 2 == 1) {
242+
return 0;
243+
}
244+
int a = 0, b = 0;
245+
for (int i = 0; i < n / 2; ++i) {
246+
a += s[i] - '0';
247+
}
248+
for (int i = n / 2; i < n; ++i) {
249+
b += s[i] - '0';
250+
}
251+
return a == b ? 1 : 0;
252+
}
253+
}
254+
```
255+
194256
<!-- tabs:end -->
195257

196258
<!-- solution:end -->

‎solution/2800-2899/2843.Count Symmetric Integers/README_EN.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,68 @@ function countSymmetricIntegers(low: number, high: number): number {
189189
}
190190
```
191191

192+
#### Rust
193+
194+
```rust
195+
impl Solution {
196+
pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
197+
let mut ans = 0;
198+
for x in low..=high {
199+
ans += Self::f(x);
200+
}
201+
ans
202+
}
203+
204+
fn f(x: i32) -> i32 {
205+
let s = x.to_string();
206+
let n = s.len();
207+
if n % 2 == 1 {
208+
return 0;
209+
}
210+
let bytes = s.as_bytes();
211+
let mut a = 0;
212+
let mut b = 0;
213+
for i in 0..n / 2 {
214+
a += (bytes[i] - b'0') as i32;
215+
}
216+
for i in n / 2..n {
217+
b += (bytes[i] - b'0') as i32;
218+
}
219+
if a == b { 1 } else { 0 }
220+
}
221+
}
222+
```
223+
224+
#### C#
225+
226+
```cs
227+
public class Solution {
228+
public int CountSymmetricIntegers(int low, int high) {
229+
int ans = 0;
230+
for (int x = low; x <= high; ++x) {
231+
ans += f(x);
232+
}
233+
return ans;
234+
}
235+
236+
private int f(int x) {
237+
string s = x.ToString();
238+
int n = s.Length;
239+
if (n % 2 == 1) {
240+
return 0;
241+
}
242+
int a = 0, b = 0;
243+
for (int i = 0; i < n / 2; ++i) {
244+
a += s[i] - '0';
245+
}
246+
for (int i = n / 2; i < n; ++i) {
247+
b += s[i] - '0';
248+
}
249+
return a == b ? 1 : 0;
250+
}
251+
}
252+
```
253+
192254
<!-- tabs:end -->
193255

194256
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int CountSymmetricIntegers(int low, int high) {
3+
int ans = 0;
4+
for (int x = low; x <= high; ++x) {
5+
ans += f(x);
6+
}
7+
return ans;
8+
}
9+
10+
private int f(int x) {
11+
string s = x.ToString();
12+
int n = s.Length;
13+
if (n % 2 == 1) {
14+
return 0;
15+
}
16+
int a = 0, b = 0;
17+
for (int i = 0; i < n / 2; ++i) {
18+
a += s[i] - '0';
19+
}
20+
for (int i = n / 2; i < n; ++i) {
21+
b += s[i] - '0';
22+
}
23+
return a == b ? 1 : 0;
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn count_symmetric_integers(low: i32, high: i32) -> i32 {
3+
let mut ans = 0;
4+
for x in low..=high {
5+
ans += Self::f(x);
6+
}
7+
ans
8+
}
9+
10+
fn f(x: i32) -> i32 {
11+
let s = x.to_string();
12+
let n = s.len();
13+
if n % 2 == 1 {
14+
return 0;
15+
}
16+
let bytes = s.as_bytes();
17+
let mut a = 0;
18+
let mut b = 0;
19+
for i in 0..n / 2 {
20+
a += (bytes[i] - b'0') as i32;
21+
}
22+
for i in n / 2..n {
23+
b += (bytes[i] - b'0') as i32;
24+
}
25+
if a == b {
26+
1
27+
} else {
28+
0
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

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