3754. Concatenate Non-Zero Digits and Multiply by Sum I
</> Solution
/**
* Problem: LeetCode 3754 - Concatenate Non-Zero Digits and Multiply by Sum I
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public long sumAndMultiply(int n) {
long x = 0;
long sum = 0;
if (n == 0) return 0;
String s = String.valueOf(n);
for (char ch : s.toCharArray()) {
if (ch != '0') {
int digit = ch - '0';
x = x * 10 + digit;
sum += digit;
}
}
return x * sum;
}
}
"""
Problem: LeetCode 3754 - Concatenate Non-Zero Digits and Multiply by Sum I
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def sumAndMultiply(self, n: int) -> int:
if n == 0:
return 0
x = 0
digit_sum = 0
for ch in str(n):
if ch != "0":
digit = ord(ch) - ord("0")
x = x * 10 + digit
digit_sum += digit
return x * digit_sum
/**
* Problem: LeetCode 3754 - Concatenate Non-Zero Digits and Multiply by Sum I
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
using namespace std;
/**
* Problem: LeetCode 3754 - Concatenate Non-Zero Digits and Multiply by Sum I
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
long long sumAndMultiply(int n) {
if (n == 0) return 0;
long long x = 0;
long long sum = 0;
string s = to_string(n);
for (char ch : s) {
if (ch != '0') {
int digit = ch - '0';
x = x * 10 + digit;
sum += digit;
}
}
return x * sum;
}
};
/**
* Problem: LeetCode 3754 - Concatenate Non-Zero Digits and Multiply by Sum I
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var sumAndMultiply = function(n) {
if (n === 0) return 0;
let x = 0;
let sum = 0;
const s = n.toString();
for (let i = 0; i < s.length; i++) {
if (s[i] !== '0') {
const digit = s.charCodeAt(i) - 48;
x = x * 10 + digit;
sum += digit;
}
}
return x * sum;
};