#3697
Easy Algorithms Compute decimal representation
Array Math
64.6% acceptance
Feb 25, 2026
47
1
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn decimal_representation(n: i32) -> Vec<i32> {
// Extract each non-zero digit with its place value.
let mut result = Vec::new();
let mut n = n;
let mut place = 1i32;
while n > 0 {
let digit = n % 10;
if digit > 0 {
result.push(digit * place);
}
n /= 10;
place *= 10;
}
result.sort_unstable_by(|a, b| b.cmp(a));
result
}
}