|
49 | 49 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
50 | 50 |
|
51 | 51 | ```python
|
52 |
| - |
| 52 | +class Solution: |
| 53 | + def countSymmetricIntegers(self, low: int, high: int) -> int: |
| 54 | + def f(x: int) -> bool: |
| 55 | + s = str(x) |
| 56 | + if len(s) & 1: |
| 57 | + return False |
| 58 | + n = len(s) // 2 |
| 59 | + return sum(map(int, s[:n])) == sum(map(int, s[n:])) |
| 60 | + |
| 61 | + return sum(f(x) for x in range(low, high + 1)) |
53 | 62 | ```
|
54 | 63 |
|
55 | 64 | ### **Java**
|
56 | 65 |
|
57 | 66 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 67 |
|
59 | 68 | ```java
|
60 |
| - |
| 69 | +class Solution { |
| 70 | + public int countSymmetricIntegers(int low, int high) { |
| 71 | + int ans = 0; |
| 72 | + for (int x = low; x <= high; ++x) { |
| 73 | + ans += f(x); |
| 74 | + } |
| 75 | + return ans; |
| 76 | + } |
| 77 | + |
| 78 | + private int f(int x) { |
| 79 | + String s = "" + x; |
| 80 | + int n = s.length(); |
| 81 | + if (n % 2 == 1) { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + int a = 0, b = 0; |
| 85 | + for (int i = 0; i < n / 2; ++i) { |
| 86 | + a += s.charAt(i) - '0'; |
| 87 | + } |
| 88 | + for (int i = n / 2; i < n; ++i) { |
| 89 | + b += s.charAt(i) - '0'; |
| 90 | + } |
| 91 | + return a == b ? 1 : 0; |
| 92 | + } |
| 93 | +} |
61 | 94 | ```
|
62 | 95 |
|
63 | 96 | ### **C++**
|
64 | 97 |
|
65 | 98 | ```cpp
|
66 |
| - |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + int countSymmetricIntegers(int low, int high) { |
| 102 | + int ans = 0; |
| 103 | + auto f = [](int x) { |
| 104 | + string s = to_string(x); |
| 105 | + int n = s.size(); |
| 106 | + if (n & 1) { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + int a = 0, b = 0; |
| 110 | + for (int i = 0; i < n / 2; ++i) { |
| 111 | + a += s[i] - '0'; |
| 112 | + b += s[n / 2 + i] - '0'; |
| 113 | + } |
| 114 | + return a == b ? 1 : 0; |
| 115 | + }; |
| 116 | + for (int x = low; x <= high; ++x) { |
| 117 | + ans += f(x); |
| 118 | + } |
| 119 | + return ans; |
| 120 | + } |
| 121 | +}; |
67 | 122 | ```
|
68 | 123 |
|
69 | 124 | ### **Go**
|
70 | 125 |
|
71 | 126 | ```go
|
| 127 | +func countSymmetricIntegers(low int, high int) (ans int) { |
| 128 | + f := func(x int) int { |
| 129 | + s := strconv.Itoa(x) |
| 130 | + n := len(s) |
| 131 | + if n&1 == 1 { |
| 132 | + return 0 |
| 133 | + } |
| 134 | + a, b := 0, 0 |
| 135 | + for i := 0; i < n/2; i++ { |
| 136 | + a += int(s[i] - '0') |
| 137 | + b += int(s[n/2+i] - '0') |
| 138 | + } |
| 139 | + if a == b { |
| 140 | + return 1 |
| 141 | + } |
| 142 | + return 0 |
| 143 | + } |
| 144 | + for x := low; x <= high; x++ { |
| 145 | + ans += f(x) |
| 146 | + } |
| 147 | + return |
| 148 | +} |
| 149 | +``` |
72 | 150 |
|
| 151 | +### **TypeScript** |
| 152 | + |
| 153 | +```ts |
| 154 | +function countSymmetricIntegers(low: number, high: number): number { |
| 155 | + let ans = 0; |
| 156 | + const f = (x: number): number => { |
| 157 | + const s = x.toString(); |
| 158 | + const n = s.length; |
| 159 | + if (n & 1) { |
| 160 | + return 0; |
| 161 | + } |
| 162 | + let a = 0; |
| 163 | + let b = 0; |
| 164 | + for (let i = 0; i < n >> 1; ++i) { |
| 165 | + a += Number(s[i]); |
| 166 | + b += Number(s[(n >> 1) + i]); |
| 167 | + } |
| 168 | + return a === b ? 1 : 0; |
| 169 | + }; |
| 170 | + for (let x = low; x <= high; ++x) { |
| 171 | + ans += f(x); |
| 172 | + } |
| 173 | + return ans; |
| 174 | +} |
73 | 175 | ```
|
74 | 176 |
|
75 | 177 | ### **...**
|
|
0 commit comments