admin管理员组文章数量:1391836
I have been trying to figure out this question from LeetCoode. This is the problem: Given an integer number n, return the difference between the product of its digits and the sum of its digits.
This is my code so far:
/**
* @param {number} n
* @return {number}
*/
//n=234
var subtractProductAndSum = function(n) {
var z= n.toString().length;
var g= n.toString()
for(var i=0; i<z; i++){
var p= g[i]+g[i+1]+g[i+2];
var y=g[i]*g[i+1]*g[i+2];
var d= y-p
}
return d;
};
I have been trying to figure out this question from LeetCoode. This is the problem: Given an integer number n, return the difference between the product of its digits and the sum of its digits.
This is my code so far:
/**
* @param {number} n
* @return {number}
*/
//n=234
var subtractProductAndSum = function(n) {
var z= n.toString().length;
var g= n.toString()
for(var i=0; i<z; i++){
var p= g[i]+g[i+1]+g[i+2];
var y=g[i]*g[i+1]*g[i+2];
var d= y-p
}
return d;
};
Share
Improve this question
edited Nov 25, 2021 at 6:54
Mureinik
313k54 gold badges364 silver badges392 bronze badges
asked Dec 31, 2019 at 17:10
Just_A_Kid_Seeking4HelpJust_A_Kid_Seeking4Help
411 silver badge2 bronze badges
6 Answers
Reset to default 1Your solution assumes the number has three digits, which is probably not what the problem intended.
I'd run over the number and extract the digits one by one, and sum and multiply them as I go. Then, just subtract the two:
var subtractProductAndSum = function(n) {
// Initialize the sum and the product with neutral values
var sum = 0;
var product = 1;
while (n > 0) {
var digit = n % 10;
sum += digit;
product *= digit;
n = Math.floor(n / 10);
}
return product - sum;
};
You can convert the integer into a string and then split it. Then using the array of digit strings, find the product of the digits and the sum of the digits by using the reduce function like so:
const subtractProductAndSum = function(n) {
const digitArray = n.toString().split('')
const digitProduct = digitArray.reduce((previousValue, currentValue) => previousValue * parseInt(currentValue), 1)
const digitSum = digitArray.reduce((previousValue, currentValue) => previousValue + parseInt(currentValue), 0)
return digitProduct - digitSum
}
You can try
/**
* @param {number} n
* @return {number}
*/
//n=234
var subtractProductAndSum = function(n) {
let product = (n+"").split("").reduce((total,curr)=>total * +curr,1);
let sum = (n+"").split("").reduce((total,curr)=>total + +curr,0);
return product - sum;
};
or
/**
* @param {number} n
* @return {number}
*/
//n=234
var subtractProductAndSum = function(n) {
let product = 1;
let sum = 0;
n = n+"";
for(let i = 0; i < n.length; i++){
product *= +n[i];
sum += +n[i];
}
return product - sum;
};
See the following :
/**
* @param {number} n
* @return {number}
*/
//n=234
var subtractProductAndSum = function(n) {
var digits = n.split('');
//console.log(digits);
var prod = digits.reduce((a, b) => a * parseInt(b), 1);
//console.log(prod);
var sum = digits.reduce((a, b) => a + parseInt(b), 0);
//console.log(sum);
//console.log(prod - sum);
return prod - sum;
};
subtractProductAndSum('234');
//it's working on IntelliJ
public class SubofProSum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sum,product,n,value,sub;
sum = 0;
product = 1;
n= in.nextInt();
while(n>0){
value=n%10;
sum = sum+value;
product = product * value;
n = n /10;
}
System.out.println("sum is: "+sum+" \nMultiple is: "+product);
sub=product-sum;
System.out.println("sub is:"+sub);
}
}
I have solved the same question in Java. I used the modulus operator (%) to pute the last digit, and the code snippet is given below.
public int subtractProductAndSum(int n) {
int productOfDigits = 1;
int sumOfDigits = 0;
while (n > 0) {
int lastDigit = n % 10;
productOfDigits *= lastDigit;
sumOfDigits += lastDigit;
n /= 10;
}
return productOfDigits - sumOfDigits;
}
本文标签: javascriptLeetCode 1281 Subtract the Product and Sum of Digits of an IntegerStack Overflow
版权声明:本文标题:javascript - LeetCode: 1281. Subtract the Product and Sum of Digits of an Integer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744771465a2624364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论