日野弥生:勉強しよう

LeetCode 1281 - 整数的各位积和之差

发表于2025年05月08日

#数学

用循环模拟数学计算即可。

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

フラッシュタブ:LeetCode

题目链接:https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

上一篇

LeetCode 258 - 各位相加

下一篇

LeetCode 242 - 有效的字母异位词