Python/algorithm

[Leetcode] 1281. Subtract the Product and Sum of Digits of an Integer

baecode 2023. 1. 4. 16:43
반응형

Problem

이번 문제는 주어진 수 n의 각 자리수를 곱한 값에 더한 값을 뺀 값을 결과로 낸다

 

나의 풀이

from functools import reduce
class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        a = list(map(int,str(n)))
        return reduce((lambda x,y: x*y), a) - sum(a)

list로 바꿔주고 functools 에 있는 reduce를 사용하여 문제를 풀었다

더한 값의 경우는 sum 을 사용하면 되지만 곱한 값의 경우는 sum 같은 부분이 없다 ㅠㅠ

 


 

reduce(sum,[1,2,3,4,5,6])

반응형