Skip to content

Commit 2edd053

Browse files
authored
Create subtract-the-product-and-sum-of-digits-of-an-integer.py
1 parent 74c81c0 commit 2edd053

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def subtractProductAndSum(self, n):
6+
"""
7+
:type n: int
8+
:rtype: int
9+
"""
10+
product, total = 1, 0
11+
while n:
12+
n, r = divmod(n, 10)
13+
product *= r
14+
total += r
15+
return product-total
16+
17+
18+
# Time: O(logn)
19+
# Space: O(logn)
20+
import operator
21+
22+
23+
class Solution2(object):
24+
def subtractProductAndSum(self, n):
25+
"""
26+
:type n: int
27+
:rtype: int
28+
"""
29+
A = map(int, str(n))
30+
return reduce(operator.mul, A) - sum(A)

0 commit comments

Comments
 (0)