Leetcode # 1056. Confusing Number

https://leetcode.com/problems/confusing-number

Solution

問題:n 是否顛倒後仍是一組數字並且不等於 n ?

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

class Solution:
  def confusingNumber(self, n: int) -> bool:
    confusing_numbers = {0: 0, 1: 1, 6: 9, 8: 8, 9: 6}
    _n = n
    rotated_n = 0
    while _n > 0:
      units_digit = _n % 10
      if units_digit not in confusing_numbers:
        return False
      rotated_n = rotated_n * 10 + confusing_numbers[units_digit]
      _n //= 10
    return n != rotated_n

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami