본문 바로가기
python-algorithm

leetcode 2180. Count Integers With Even Digit Sum

by 무적김두칠 2023. 1. 12.

https://leetcode.com/problems/count-integers-with-even-digit-sum/description/

 

Count Integers With Even Digit Sum - LeetCode

Count Integers With Even Digit Sum - Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even. The digit sum of a positive integer is the sum of all its digits.   Example 1: Input: num = 4 Ou

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def countEven(self, num: int-> int:
        answer = 0
        for i in range(1, num+1):
            str_integer = str(i)
            digit_sum = 0
            for num in str_integer:
                digit_sum +=int(num)
            if digit_sum %2 == 0:
                answer+=1
        
        return answer
 
cs
반응형

댓글