2180. 统计各位数字之和为偶数的整数个数

摘要
Title: 2180. 统计各位数字之和为偶数的整数个数
Tag: 模拟
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

2180. 统计各位数字之和为偶数的整数个数

  • 题意

    给你一个正整数 num ,请你统计并返回 小于或等于 num 且各位数字之和为 偶数 的正整数的数目。
    正整数的 各位数字之和 是其所有位上的对应数字相加的结果。

  • 思路

    数据范围小,模拟

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class Solution:
    def countEven(self, num: int) -> int:
    res = 0
    for i in range(1, num + 1):
    s = str(i)
    cnt = 0
    for j in s:
    cnt += int(j)
    if cnt % 2 == 0:
    res += 1
    return res
使用搜索:谷歌必应百度