给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。
输入数据范围:
1 <= nums.length <= 500
1 <= nums[i] <= 10^5
解1:直接法:
class Solution {
public:
bool countDigits(int num) {
int count = 0;
while (num) {
num /= 10;
++count;
}
return !(count & 1);
}
int findNumbers(vector<int>& nums) {
int res = 0;
for (int num : nums) {
res += countDigits(num);
}
return res;
}
};
解2:利用数据范围:
class Solution {
public:
int findNumbers(vector<int>& nums) {
int res = 0;
for (int num : nums) {
if (num <= 99 && num >= 10 || num >=1000 && num <= 9999 || num == 100000) {
++res;
}
}
return res;
}
};
解3:使用to_string函数:
class Solution {
public:
int findNumbers(vector<int>& nums) {
int res = 0;
for (int num : nums) {
if (!(to_string(num).size() & 1)) {
++res;
}
}
return res;
}
};