LeetCode 1295. Statistical even number of digits to digital (C ++ description)

1295. Statistical even number of digits to digital

Difficulty simply
give you an array of integers nums, you return to where the median of an even number of numbers.

Example 1:

Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 is 2 digits (number of bits is an even number)
345 3 digits (digits is odd)
2 is a 1-bit digital (odd digits )
6 is an odd number of digits)
7896 4 digits (number of bits is an even number)
so that only the 7896 is 12 bits and an even number

Example 2:

Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only median number 1771 is an even number.

prompt:

1 <= nums.length <= 500
1 <= nums[i] <= 10^5

See this problem when the first thought is through a loop to determine the number of bits of each number, and then a judge, the code is:

class Solution {
public:
    int solove(int num){
        int re = 0;
        while(num){
            num /= 10;
            re++;
        }
        return re;
    }
    int findNumbers(vector<int>& nums) {
        int ans = 0;
        for(int i = 0; i < nums.size(); i++)
            if(solove(nums[i]) % 2 == 0)
                ans++;
        return ans;
    }
};

This question is a very plus single topic, we just need to get it to perform a number of bits to judge, of course, there is no deep step of optimizing time efficiency, time efficiency is still room for optimization of

Published 153 original articles · won praise 247 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43422111/article/details/105179875