forked from nirmalnishant645/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cd537e
commit 02ab986
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
1281-Subtract-the-Product-and-Sum-of-Digits-of-an-integer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
''' | ||
Given an integer number n, return the difference between the product of its digits and the sum of its digits. | ||
Example 1: | ||
Input: n = 234 | ||
Output: 15 | ||
Explanation: | ||
Product of digits = 2 * 3 * 4 = 24 | ||
Sum of digits = 2 + 3 + 4 = 9 | ||
Result = 24 - 9 = 15 | ||
Example 2: | ||
Input: n = 4421 | ||
Output: 21 | ||
Explanation: | ||
Product of digits = 4 * 4 * 2 * 1 = 32 | ||
Sum of digits = 4 + 4 + 2 + 1 = 11 | ||
Result = 32 - 11 = 21 | ||
Constraints: | ||
1 <= n <= 10^5 | ||
''' | ||
class Solution: | ||
def subtractProductAndSum(self, n: int) -> int: | ||
digit, s, p = 0, 0, 1 | ||
while n > 0: | ||
digit = n%10 | ||
n = n//10 | ||
p = p * digit | ||
s = s + digit | ||
return p - s |