본문 바로가기
python-algorithm

leetcode 2843. Count Symmetric Integers

by 무적김두칠 2024. 1. 5.

https://leetcode.com/problems/count-symmetric-integers/description/

 

Count Symmetric Integers - LeetCode

Can you solve this real interview question? Count Symmetric Integers - You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def countSymmetricIntegers(self, low: int, high: int-> int:
        answer = 0
        for num in range(low, high + 1):
            num = str(num)
            if len(num) % 2 == 0:
                left_sum = sum(map(int, num[:len(num)//2]))
                right_sum = sum(map(int, num[len(num)//2:]))
                if left_sum == right_sum:
                    answer += 1
        return answer
        
cs
반응형

댓글