LeetCode(1281) - Subtract the Product and Sum of Digits of an Integer

IT/알고리즘 2020.02.25 댓글 Hyunyoung Kim

정수 번호가 주어졌을 때, 각 자릿수의 숫자를 곱한 값과, 각 자릿수의 숫자를 더한 값의 차이를 return해라

 

 

Pyhton

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        sumResult=0
        productResult=1
        for n in str(n):
            sumResult+=int(n)
            productResult*=int(n)
        return productResult-sumResult

 

Javscript

var subtractProductAndSum = function(n) {
    let sumResult=0;
    let productResult=1;
    for(let i of String(n)){
        sumResult+=parseInt(i)
        productResult*=parseInt(i)
    }
    return productResult-sumResult
};

댓글