Leetcode # 2843. Count Symmetric Integers

Problem

https://leetcode.com/contest/weekly-contest-361/problems/count-symmetric-integers/

Solution

三位數(ex: 0303)不算

Time Complexity: O(1)
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def countSymmetricIntegers(self, low: int, high: int) -> int:
    ans = len([True for i in range(1, 10) if low <= i * 11 <= high])
    for i in range(max(10, low // 100), ceil(high / 100)):
      d1, d2 = i // 10, i % 10
      for d3 in range(min(10, d1 + d2 + 1)):
        d4 = d1 + d2 - d3
        if d4 > 9: continue
        num = d1 * 1000 + d2 * 100 + d3 * 10 + d4
        if low <= num <= high:
          ans += 1
          # print(d1, d2, d3, d4, num)
          
    return ans

Last Updated on 2023/09/10 by A1go

目錄
Bitnami