preface

It’s Friday afternoon. Have you finished writing your requirements for this week? Really envy the big guys still need to finish writing!

Unlike me, wake up in the afternoon and wait to go off work 🐶 that write problem solution 😄

Subject to introduce

The difference between the sum of the product of integers

Give you an integer n, ask you to help calculate and return the integer “product of digits” and “sum of digits” difference.

Example 1:

Input: n = 234 Output: 15 Description: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24-9 = 15Copy the code

Example 2:

Input: n = 4421 Output: 21 Description: Product of digits = 4 * 4 * 2 * 1 = 32 Sum of digits = 4 + 4 + 2 + 1 = 11 Result = 32-11 = 21Copy the code

Tip:

  • 1 <= n <=
    1 0 5 10^5

Their thinking

Find the product and sum of the digits of an integer n!

There are two basic solutions to this problem:

Turn the string

Converting a string is simple:

  • Convert n to the string STR.

  • Loop string STR, each value STR [I] is added and multiplied in turn (note the treatment of STR [I]).

  • And then you get the sum of the products that they asked for, and then you just return the difference.

Do not convert string

This problem is basically how to get every digit of n, so if we don’t convert n to a string, we can solve this problem by using % mod.

Set sum to the sum, total to the product, and both initial values to 0.

  • n % 10Can take tonThe ones place of theta corresponds to thetasumandtotalSave it.
  • We got that in the last stepnThe ones place of theta, so this is where we get rid of the values we already gotMath.floor(n / 10), and then continue with the previous step.
  • Until I’m not satisfiedn > 0The case ends and returnssum - totalCan.

The eval function

When I was looking at the solution, I found a very clever solution, can be said to be clever with JavaScript, posted to show.

  • Take the n-turn string and split it into an array.

  • Concatenate arrays into strings using * and +.

  • Execute strings using the eval function.

The problem solving code

Turn the string

var subtractProductAndSum = function(n) {
    var str = n.toString();
    var sum = 0,total = 1;
    for(let i = 0 ; i< str.length ; i++){
        sum = sum + parseInt(str[i]);
        total = total * parseInt(str[i]);
    }
    return total - sum;
}
Copy the code

Do not convert string

var subtractProductAndSum = function(n) {
    let sum = 0;
    let total = 1;
    while (n) {
        sum = n % 10 + sum;
        total = n % 10 * total;
        n = Math.floor(n/10);
    }
    return total - sum;
}
Copy the code

The eval function

var subtractProductAndSum = function(n) {
    let arr = n.toString().split('');
    let a = eval(arr.join('*'));
    let b = eval(arr.join('+'));
    return a-b;
};
Copy the code

Swipe questions and punch out records

Here is the previous swipe card records, you can have a look, if you have any different views and views or feel what is wrong, welcome to leave a message in the comment area! 🙏 🙏 🙏

[LeetCode0303 topic area and retrieval – array immutable] | punch brush

[LeetCode1200. Minimum absolute difference] | punch brush

[LeetCode0304 topic 2 d area and retrieval – matrix immutable] | punch brush

[LeetCode11 topic containers of most water] | punch brush

[LeetCode0338 topic bit count] | punch brush

[LeetCode209 topic length minimum subarray] | punch brush

[LeetCode236 topic in recent common ancestor of binary tree] | punch brush

[LeetCode141 topic circular linked list] | punch brush

[LeetCode53 topic most architectural sequence and] | punch brush

[LeetCode48 topic rotating images] | punch brush

[LeetCode155 topic minimum stack] | punch brush

[LeetCode1124 problem well, the longest period of a] | punch brush

[LeetCode274 problem H index] | punch brush

[LeetCode367 problem effective completely square number] | punch brush

[LeetCode1047 problem delete all adjacent duplicates of the string] | punch brush

[LeetCode160 topic cross linked list] | punch brush

[LeetCode1438 topic absolute difference does not exceed the limit of the longest continuous subarray] | punch brush

[LeetCode434 problem the number of words in a string] | punch brush

[LeetCode75 topic classification of color] | punch brush

[LeetCode513 problem to find the value of the trees left] | punch brush

[LeetCode94 title sequence in the binary tree traversal] | punch brush

[LeetCode617 topic merger binary tree] | punch brush

[LeetCode331 problem to verify the preamble of binary tree serialization] | punch brush

conclusion

Come on! HXDM!!!!!! 💪 💪 💪

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign