1295. Find Numbers with Even Number of Digits #1625
-
Topics: Given an array Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine how many numbers in a given array have an even number of digits. ApproachThe approach involves converting each number in the array to a string and checking the length of that string. If the length is even, we count that number. This method is straightforward and efficient given the constraints.
Let's implement this solution in PHP: 1295. Find Numbers with Even Number of Digits <?php
/**
* @param Integer[] $nums
* @return Integer
*/
function findNumbers($nums) {
$count = 0;
foreach ($nums as $num) {
$numStr = (string)$num;
if (strlen($numStr) % 2 == 0) {
$count++;
}
}
return $count;
}
// Test Cases
echo findNumbers([12, 345, 2, 6, 7896]) . "\n"; // Output: 2
echo findNumbers([555, 901, 482, 1771]) . "\n"; // Output: 1
?> Explanation:
This approach efficiently handles the problem constraints and ensures that we correctly count the numbers with even digit lengths using straightforward string conversion and length checking. |
Beta Was this translation helpful? Give feedback.
We need to determine how many numbers in a given array have an even number of digits.
Approach
The approach involves converting each number in the array to a string and checking the length of that string. If the length is even, we count that number. This method is straightforward and efficient given the constraints.
Let's implement this solution in PHP: 1295. Find Numbers with Even Num…