File tree 1 file changed +20
-10
lines changed 1 file changed +20
-10
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int countSymmetricIntegers (int low , int high ) {
3
- int result = 0 ;
3
+ int count = 0 ;
4
4
for (int i = low ; i <= high ; i ++) {
5
- String s = String .valueOf (i );
6
- int diff = 0 ;
7
- int n = s .length ();
8
- for (int j = 0 ; j < n / 2 ; j ++) {
9
- diff += s .charAt (j ) - s .charAt (n - j - 1 );
10
- }
11
- if (n % 2 == 0 && diff == 0 ) {
12
- result ++;
5
+ if (isSymmetric (i )) {
6
+ count ++;
13
7
}
14
8
}
15
- return result ;
9
+ return count ;
10
+ }
11
+
12
+ private static boolean isSymmetric (int num ) {
13
+ String s = String .valueOf (num );
14
+ int n = s .length ();
15
+ if (n % 2 != 0 ) {
16
+ return false ;
17
+ }
18
+ int sum = 0 ;
19
+ for (int i = 0 ; i < n / 2 ; i ++) {
20
+ sum += Character .getNumericValue (s .charAt (i ));
21
+ }
22
+ for (int i = n / 2 ; i < n ; i ++) {
23
+ sum -= Character .getNumericValue (s .charAt (i ));
24
+ }
25
+ return sum == 0 ;
16
26
}
17
27
}
You can’t perform that action at this time.
0 commit comments