LeetCode281周赛: 6012. 统计各位数字之和为偶数的整数个数

153 阅读1分钟

原题地址: leetcode-cn.com/problems/co…

image.png

class Solution {
    public int countEven(int num) {
        int count = 0, index = 2;
        while (index <= num) {
            int temp = index;
            int i = 0;
            while (temp != 0) {
                i += temp % 10;
                temp = temp / 10;
            }
            if (i % 2 == 0) {
                count ++;
            }
            index ++;
        }
        return count;
    }
}

思路: 遍历直接从2开始, 对每一位数相加判断即可