leetcode-1281 整数的各位积和之差 Python

方法一:取余

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        sum = 0
        mul = 1
        while n:
            num = n % 10
            n //= 10
            sum += num
            mul *= num
        return mul - sum
        

方法二:转字符串

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        sum = 0
        mul = 1
        for i in str(n):
            sum += int(i)
            mul *= int(i)
        return mul - sum    

你可能感兴趣的:(python,算法,leetcode)