Skip to content

Commit fdd13d5

Browse files
committed
Find Numbers with Even Number of Digits
1 parent 6ca628e commit fdd13d5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
3+
4+
Given an array nums of integers, return how many of them contain an even number of digits.
5+
6+
Example 1:
7+
Input: nums = [12,345,2,6,7896]
8+
Output: 2
9+
Explanation:
10+
12 contains 2 digits (even number of digits).
11+
345 contains 3 digits (odd number of digits).
12+
2 contains 1 digit (odd number of digits).
13+
6 contains 1 digit (odd number of digits).
14+
7896 contains 4 digits (even number of digits).
15+
Therefore only 12 and 7896 contain an even number of digits.
16+
17+
Example 2:
18+
Input: nums = [555,901,482,1771]
19+
Output: 1
20+
Explanation:
21+
Only 1771 contains an even number of digits.
22+
23+
Constraints:
24+
1 <= nums.length <= 500
25+
1 <= nums[i] <= 10^5
26+
"""
27+
class Solution:
28+
def findNumbers(self, nums: List[int]) -> int:
29+
return sum(self.number_of_digits(number) for number in nums)
30+
31+
32+
def number_of_digits(self, n):
33+
count = 0
34+
while n:
35+
count += 1
36+
n //= 10
37+
return 1 if count % 2 == 0 else 0

0 commit comments

Comments
 (0)