Convert each positive integer to its decimal representation, test whether its length is even, and count the matches.
Complexity
O(n * d) time, where d is the maximum digit count, and O(1) auxiliary space.
Count the integers whose decimal representation contains an even number of digits.
Convert each positive integer to its decimal representation, test whether its length is even, and count the matches.
O(n * d) time, where d is the maximum digit count, and O(1) auxiliary space.
package main
func findNumbers(nums []int) int {
count := 0
for _, number := range nums {
digits := 0
for value := number; value > 0; value /= 10 {
digits++
}
if digits%2 == 0 {
count++
}
}
return count
}
class Solution:
def findNumbers(self, nums: list[int]) -> int:
return sum(len(str(number)) % 2 == 0 for number in nums)
function findNumbers(nums: number[]): number {
return nums.reduce(
(count, number) => count + (String(number).length % 2 === 0 ? 1 : 0),
0,
);
}