반응형
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])
반응형
'Python > algorithm' 카테고리의 다른 글
[Leetcode] 2236. Root Equals Sum of Children (1) | 2023.01.11 |
---|---|
[Leetcode] 1365. How Many Numbers Are Smaller Than the Current Number (1) | 2023.01.05 |
[Leetcode] 1431. Kids With the Greatest Number of Candies (1) | 2023.01.02 |
[Leetcode] 2413. Smallest Even Multiple (1) | 2022.12.31 |
[Leetcode] 1603. Design Parking System (1) | 2022.12.30 |