题目
Given an array nums of integers, return how many of them contain an even number of digits.
解题思路
- 转换成字符串,求长度
- 求每次除以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