Skip to content
the poor coder

Leetcode - Find Numbers with Even Number of Digits Solution

Poor Man
Published
Updated
Reading time
1 min read

Given an array nums of integers, return how many of them contain an even number of digits.

Example 1:

Text
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.

Example 2:

Text
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

Solution in Python#

Python
class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return sum((len(str(num))+1)%2 for num in nums)

Keep going

2021

Leetcode - Find the Highest Altitude Solution

There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0. You are given an integer array gain of length n where…

leetcodearray1 min read
2021

Leetcode - Average Waiting Time Solution

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]: arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order…

leetcodearray4 min read
ESC