[LeetCode] Concatenate Non-Zero Digits and Multiply by Sum I

3754. Concatenate Non-Zero Digits and Multiply by Sum I

You are given an integer n.

Form a new integer x by concatenating all the non-zero digits of n in their original order. If there are no non-zero digits, x = 0.

Let sum be the sum of digits in x.

Return an integer representing the value of x * sum.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
long long sumAndMultiply(int n) {
long long sum = 0, concat = 0;
for(auto& ch : to_string(n)) {
int x = ch - '0';
if(x) {
sum += x, concat = concat * 10 + x;
}
}
return sum * concat;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/concatenate-non-zero-digits-and-multiply-by-sum-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.