---
tags: LeetCode
---
# 1295. Find Numbers with Even Number of Digits
Given an array nums of integers, return how many of them contain an even number of digits.
1. Example 1:
- Input: nums = [12,345,2,6,7896]
- Output: 2
- Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.
2. Example 2:
- Input: nums = [555,901,482,1771]
- Output: 1
- Explanation:
Only 1771 contains an even number of digits.
Constraints:
- 1 <= nums.length <= 500
- 1 <= nums[i] <= 10^5
---
輸入範本如下
```C#
public class Solution {
public int FindNumbers(int[] nums)
{
}
}
```
自行限制 , 不准使用 LinQ
```
public int FindNumbers(int[] nums)
{
return nums.Count(num =>
{
bool isEven = true;
while (num > 0)
{
num /= 10;
isEven = !isEven;
}
return isEven;
});
```
```C#
public int FindNumbers(int[] nums) => nums.Count(num =>
num.ToString().Length % 2 == 0);
```
### 直覺想法
1. 第一個想法是我要怎麼知道一個數字的位數是多少!?
因為數字是十進位的 , 所以一個數字一直除以 10 可以除幾次 ,應該就代表它的位數了吧.
```C#
public int FindNumbers(int[] nums)
{
int count = 0;
foreach (var i in nums)
{
if (IsEvenDigit(i))
{
count++;
}
}
return count;
bool IsEvenDigit(int num)
{
bool isEven = true;
while (num > 0)
{
num /= 10;
isEven = !isEven;
}
return isEven;
}
}
```
2. 後來的想法是將數字轉成字串 , 自然也就可以知道它的位數是多少了
```C#
public int FindNumbers(int[] nums)
{
int count = 0;
foreach (var i in nums)
{
if (IsEvenDigit(i))
{
count++;
}
}
return count;
bool IsEvenDigit(int num) => num.ToString().Length % 2 == 0;
}
```
### Thank you!
You can find me on
- [GitHub](https://github.com/s0920832252)
- [Facebook](https://www.facebook.com/fourtune.chen)
若有謬誤 , 煩請告知 , 新手發帖請多包涵
# :100: :muscle: :tada: :sheep: