Skip to content

Commit a2dec7f

Browse files
committed
leetcode_1281_Subtract_the_Product_and_Sum_of_Digits_of_an_Integer
1 parent b073ba6 commit a2dec7f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 给定一个正数,求各位之积减去各位之和。
3+
*
4+
* AC: Runtime: 0 ms | Memory Usage: 8.3 MB
5+
*/
6+
class Solution {
7+
public:
8+
int subtractProductAndSum(int n) {
9+
if(n == 0)
10+
return 0;
11+
int product = 1, sum = 0;
12+
while(n != 0) {
13+
product *= n % 10;
14+
sum += n % 10;
15+
n /= 10;
16+
}
17+
18+
return product - sum;
19+
}
20+
};

0 commit comments

Comments
 (0)