2025-04-11 Daily Challenge
Today I have done leetcode's April LeetCoding Challenge with cpp
.
April LeetCoding Challenge 11
Description
Count Symmetric Integers
You are given two positive integers low
and high
.
An integer x
consisting of 2 * n
digits is symmetric if the sum of the first n
digits of x
is equal to the sum of the last n
digits of x
. Numbers with an odd number of digits are never symmetric.
Return the number of symmetric integers in the range [low, high]
.
Example 1:
Input: low = 1, high = 100 Output: 9 Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Example 2:
Input: low = 1200, high = 1230 Output: 4 Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
Constraints:
1 <= low <= high <= 104
Solution
class Solution {
static vector<int> result;
public:
int countSymmetricIntegers(int low, int high) {
return result[high] - result[low - 1];
}
};
vector<int> Solution::result = []() {
vector<int> result(10001);
for(int i = 11; i < 10001; ++i) {
result[i] = result[i - 1];
if((i < 100 && i / 10 == i % 10) || (i > 1000 && (i / 1000 + i / 100 % 10 == i % 10 + i / 10 % 10))) result[i] += 1;
}
return result;
}();
// Accepted
// 1967/1967 cases passed (4 ms)
// Your runtime beats 91.74 % of cpp submissions
// Your memory usage beats 84.15 % of cpp submissions (9.6 MB)