LeetCode 1281: Subtract the Product and Sum of Digits of an Integer (Single-Pass Digit Decomposition)
LeetCode 1281MathDigit ProcessingToday we solve LeetCode 1281 - Subtract the Product and Sum of Digits of an Integer.
Source: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
English
Problem Summary
Given an integer n, repeatedly take its last digit, compute the product of all digits and the sum of all digits, then return product - sum.
Key Insight
Each step can extract one digit with digit = n % 10 and remove it via n /= 10. While extracting, update both accumulators at the same time: multiply into product and add into sum.
Algorithm
- Initialize product = 1, sum = 0.
- While n > 0: get digit = n % 10, do product *= digit, sum += digit, then n /= 10.
- Return product - sum.
Complexity Analysis
Let d be the number of digits in n.
Time: O(d).
Space: O(1).
Common Pitfalls
- Initializing product to 0 (must be 1).
- Forgetting integer division when removing digits.
- Mixing up sum - product vs required product - sum.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int subtractProductAndSum(int n) {
int product = 1;
int sum = 0;
while (n > 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}
return product - sum;
}
}func subtractProductAndSum(n int) int {
product := 1
sum := 0
for n > 0 {
digit := n % 10
product *= digit
sum += digit
n /= 10
}
return product - sum
}class Solution {
public:
int subtractProductAndSum(int n) {
int product = 1, sum = 0;
while (n > 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}
return product - sum;
}
};class Solution:
def subtractProductAndSum(self, n: int) -> int:
product = 1
total = 0
while n > 0:
digit = n % 10
product *= digit
total += digit
n //= 10
return product - totalvar subtractProductAndSum = function(n) {
let product = 1;
let sum = 0;
while (n > 0) {
const digit = n % 10;
product *= digit;
sum += digit;
n = Math.floor(n / 10);
}
return product - sum;
};中文
题目概述
给定整数 n,需要取出它的每一位数字,分别计算“各位数字乘积”和“各位数字和”,最后返回 乘积 - 和。
核心思路
每次用 n % 10 取出末位数字,用 n /= 10 去掉末位。遍历过程中同时维护两个量:product 与 sum,一次循环同时更新即可。
算法步骤
- 初始化 product = 1、sum = 0。
- 当 n > 0 时:digit = n % 10,然后执行 product *= digit 与 sum += digit,再令 n /= 10。
- 循环结束后返回 product - sum。
复杂度分析
设 n 的位数为 d。
时间复杂度:O(d)。
空间复杂度:O(1)。
常见陷阱
- 把 product 初值写成 0(应为 1)。
- 去除末位时忘记使用整除。
- 把返回值写成 sum - product。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int subtractProductAndSum(int n) {
int product = 1;
int sum = 0;
while (n > 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}
return product - sum;
}
}func subtractProductAndSum(n int) int {
product := 1
sum := 0
for n > 0 {
digit := n % 10
product *= digit
sum += digit
n /= 10
}
return product - sum
}class Solution {
public:
int subtractProductAndSum(int n) {
int product = 1, sum = 0;
while (n > 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}
return product - sum;
}
};class Solution:
def subtractProductAndSum(self, n: int) -> int:
product = 1
total = 0
while n > 0:
digit = n % 10
product *= digit
total += digit
n //= 10
return product - totalvar subtractProductAndSum = function(n) {
let product = 1;
let sum = 0;
while (n > 0) {
const digit = n % 10;
product *= digit;
sum += digit;
n = Math.floor(n / 10);
}
return product - sum;
};
Comments