LeetCode 1295: Find Numbers with Even Number of Digits (Digit Counting by Division)
LeetCode 1295ArrayMathDigit CountToday we solve LeetCode 1295 - Find Numbers with Even Number of Digits.
Source: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
English
Problem Summary
Given an integer array nums, return how many numbers have an even number of decimal digits.
Key Insight
For each number, only digit-count parity matters. Count digits with repeated division by 10, then test digits % 2 == 0.
Brute Force and Limitations
A brute approach converts each number to string and checks string length. It works in O(n) but allocates extra string objects. Division-based counting avoids conversion overhead.
Optimal Algorithm Steps
1) Initialize answer ans = 0.
2) For each x in nums, count its digits by repeatedly doing x /= 10 until x == 0.
3) If digit count is even, increment ans.
4) Return ans.
Complexity Analysis
Time: O(n * d), where d is max digit count (at most 5 under constraints).
Space: O(1).
Common Pitfalls
- Forgetting that 0 has one digit.
- Using floating-point log10 and hitting precision corner cases.
- Not handling negative values safely if extending beyond original constraints.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int x : nums) {
if ((digitCount(x) & 1) == 0) ans++;
}
return ans;
}
private int digitCount(int x) {
x = Math.abs(x);
if (x == 0) return 1;
int cnt = 0;
while (x > 0) {
x /= 10;
cnt++;
}
return cnt;
}
}func findNumbers(nums []int) int {
ans := 0
for _, x := range nums {
if digitCount(x)%2 == 0 {
ans++
}
}
return ans
}
func digitCount(x int) int {
if x < 0 {
x = -x
}
if x == 0 {
return 1
}
cnt := 0
for x > 0 {
x /= 10
cnt++
}
return cnt
}class Solution {
public:
int findNumbers(vector& nums) {
int ans = 0;
for (int x : nums) {
if (digitCount(x) % 2 == 0) ++ans;
}
return ans;
}
private:
int digitCount(int x) {
x = abs(x);
if (x == 0) return 1;
int cnt = 0;
while (x > 0) {
x /= 10;
++cnt;
}
return cnt;
}
}; class Solution:
def findNumbers(self, nums: List[int]) -> int:
ans = 0
for x in nums:
if self.digit_count(x) % 2 == 0:
ans += 1
return ans
def digit_count(self, x: int) -> int:
x = abs(x)
if x == 0:
return 1
cnt = 0
while x > 0:
x //= 10
cnt += 1
return cntvar findNumbers = function(nums) {
let ans = 0;
for (const x of nums) {
if (digitCount(x) % 2 === 0) ans++;
}
return ans;
};
function digitCount(x) {
x = Math.abs(x);
if (x === 0) return 1;
let cnt = 0;
while (x > 0) {
x = Math.floor(x / 10);
cnt++;
}
return cnt;
}中文
题目概述
给定整数数组 nums,返回其中十进制位数为偶数的数字个数。
核心思路
每个数字只关心“位数奇偶性”。通过不断除以 10 统计位数,再判断 digits % 2 == 0 即可。
暴力解法与不足
可以先把数字转字符串,按长度判断偶数位。这是可行的 O(n) 解法,但会有额外字符串分配;用除法计数更直接。
最优算法步骤
1)初始化答案 ans = 0。
2)遍历 nums 中每个 x,循环执行 x /= 10 统计位数,直到 x == 0。
3)若位数为偶数,ans++。
4)返回 ans。
复杂度分析
时间复杂度:O(n * d),d 为最大位数(本题约束下最多 5)。
空间复杂度:O(1)。
常见陷阱
- 忘记 0 的位数是 1。
- 用 log10 处理时可能遇到浮点精度边界问题。
- 若扩展到负数输入,未先取绝对值会出错。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int x : nums) {
if ((digitCount(x) & 1) == 0) ans++;
}
return ans;
}
private int digitCount(int x) {
x = Math.abs(x);
if (x == 0) return 1;
int cnt = 0;
while (x > 0) {
x /= 10;
cnt++;
}
return cnt;
}
}func findNumbers(nums []int) int {
ans := 0
for _, x := range nums {
if digitCount(x)%2 == 0 {
ans++
}
}
return ans
}
func digitCount(x int) int {
if x < 0 {
x = -x
}
if x == 0 {
return 1
}
cnt := 0
for x > 0 {
x /= 10
cnt++
}
return cnt
}class Solution {
public:
int findNumbers(vector& nums) {
int ans = 0;
for (int x : nums) {
if (digitCount(x) % 2 == 0) ++ans;
}
return ans;
}
private:
int digitCount(int x) {
x = abs(x);
if (x == 0) return 1;
int cnt = 0;
while (x > 0) {
x /= 10;
++cnt;
}
return cnt;
}
}; class Solution:
def findNumbers(self, nums: List[int]) -> int:
ans = 0
for x in nums:
if self.digit_count(x) % 2 == 0:
ans += 1
return ans
def digit_count(self, x: int) -> int:
x = abs(x)
if x == 0:
return 1
cnt = 0
while x > 0:
x //= 10
cnt += 1
return cntvar findNumbers = function(nums) {
let ans = 0;
for (const x of nums) {
if (digitCount(x) % 2 === 0) ans++;
}
return ans;
};
function digitCount(x) {
x = Math.abs(x);
if (x === 0) return 1;
let cnt = 0;
while (x > 0) {
x = Math.floor(x / 10);
cnt++;
}
return cnt;
}
Comments