Question 1281. Subtract the Product and Sum of Digits of an Integer

Link:

https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

My code:

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        IntLength = math.floor(math.log10(n))+1
        Sum = 0
        Prod =1
        Num = n
        for i in range(IntLength):
            Digit = Num %10
            Num = (Num-Digit)/10
            Sum += Digit
            Prod *= Digit
        Diff =int(Prod - Sum)
        return Diff

Explanation:

The idea is clear , just take out allthe digits and then calculate the result.

Leave a comment