LeetCode 1295 Find Numbers with Even Number of Digits (Python)

Posted by 小明MaxMing on February 21, 2022

题目

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

解题思路

  1. 转换成字符串,求长度
  2. 求每次除以10,直到0的次数

代码

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return sum(len(str(n)) % 2 == 0 for n in nums)
class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        res = 0
        for n in nums:
            tmp = 0
            while n > 0:
                tmp += 1
                n //= 10
            if tmp % 2 == 0:
                res += 1

        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅