Link Search Menu Expand Document

1295. Find Numbers with Even Number of Digits

Solution Code

C#

public class Solution {
    public int CountDigit(int n) {
        return (int)Math.Floor(Math.Log10(n) + 1);
    }
    
    public int FindNumbers(int[] nums) {
        int ans = 0, n = nums.Length;
        for (int i = 0; i < n; i++) {
            if (CountDigit(nums[i]) % 2 == 0)
                ans++;
        }
        return ans;
    }
}

C++

class Solution {
public:
    int countDigit(int n) {
        return floor(log10(n) + 1);
    }
    
    int findNumbers(vector<int>& nums) {
        int ans = 0, n = nums.size();
        for (int i = 0; i < n; i++) {
            if (!(countDigit(nums[i]) & 1))
                ans++;
        }
        return ans;
    }
};

Java

class Solution {
    public int countDigit(int n) {
        return (int)Math.floor(Math.log10(n) + 1);
    }
    
    public int findNumbers(int[] nums) {
        int ans = 0, n = nums.length;
        for (int i = 0; i < n; i++) {
            if (countDigit(nums[i]) % 2 == 0)
                ans++;
        }
        return ans;
    }
}

© 2023. All rights reserved.