[LeetCode] Compute Decimal Representation

3697. Compute Decimal Representation

You are given a positive integer n.

A positive integer is a base-10 component if it is the product of a single digit from 1 to 9 and a non-negative power of 10. For example, 500, 30, and 7 are base-10 components, while 537, 102, and 11 are not.

Express n as a sum of only base-10 components, using the fewest base-10 components possible.

Return an array containing these base-10 components in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
vector<int> decimalRepresentation(int n) {
vector<int> res;
long long base = 1;
while (n > 0) {
int d = n % 10;
if (d) res.push_back(d * base);
n /= 10;
base *= 10;
}
sort(res.begin(), res.end(), greater<int>());
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/10/11/PS/LeetCode/compute-decimal-representation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.