Skip to content

Commit 1ececd1

Browse files
committed
feat: add solutions to lc problem: No.1281. Subtract the Product and Sum
of Digits of an Integer
1 parent 134d36f commit 1ececd1

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
<li><code>1 &lt;= n &lt;= 10^5</code></li>
3939
</ul>
4040

41-
4241
## 解法
4342

4443
<!-- 这里可写通用的实现逻辑 -->
@@ -50,15 +49,67 @@
5049
<!-- 这里可写当前语言的特殊实现逻辑 -->
5150

5251
```python
53-
52+
class Solution:
53+
def subtractProductAndSum(self, n: int) -> int:
54+
s, p = 0, 1
55+
while n:
56+
t = n % 10
57+
n //= 10
58+
s += t
59+
p *= t
60+
return p - s
5461
```
5562

5663
### **Java**
5764

5865
<!-- 这里可写当前语言的特殊实现逻辑 -->
5966

6067
```java
68+
class Solution {
69+
public int subtractProductAndSum(int n) {
70+
int s = 0, p = 1;
71+
while (n != 0) {
72+
int t = n % 10;
73+
n /= 10;
74+
s += t;
75+
p *= t;
76+
}
77+
return p - s;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int subtractProductAndSum(int n) {
88+
int s = 0, p = 1;
89+
while (n) {
90+
int t = n % 10;
91+
n /= 10;
92+
s += t;
93+
p *= t;
94+
}
95+
return p - s;
96+
}
97+
};
98+
```
6199
100+
### **Go**
101+
102+
```go
103+
func subtractProductAndSum(n int) int {
104+
s, p := 0, 1
105+
for n != 0 {
106+
t := n % 10
107+
n /= 10
108+
s += t
109+
p *= t
110+
}
111+
return p - s
112+
}
62113
```
63114

64115
### **...**

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Description
66

77
Given an integer number <code>n</code>, return the difference between the product of its digits and the sum of its digits.
8+
89
<p>&nbsp;</p>
910
<p><strong>Example 1:</strong></p>
1011

@@ -35,21 +36,72 @@ Result = 32 - 11 = 21
3536
<li><code>1 &lt;= n &lt;= 10^5</code></li>
3637
</ul>
3738

38-
3939
## Solutions
4040

4141
<!-- tabs:start -->
4242

4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def subtractProductAndSum(self, n: int) -> int:
48+
s, p = 0, 1
49+
while n:
50+
t = n % 10
51+
n //= 10
52+
s += t
53+
p *= t
54+
return p - s
4755
```
4856

4957
### **Java**
5058

5159
```java
60+
class Solution {
61+
public int subtractProductAndSum(int n) {
62+
int s = 0, p = 1;
63+
while (n != 0) {
64+
int t = n % 10;
65+
n /= 10;
66+
s += t;
67+
p *= t;
68+
}
69+
return p - s;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int subtractProductAndSum(int n) {
80+
int s = 0, p = 1;
81+
while (n) {
82+
int t = n % 10;
83+
n /= 10;
84+
s += t;
85+
p *= t;
86+
}
87+
return p - s;
88+
}
89+
};
90+
```
5291
92+
### **Go**
93+
94+
```go
95+
func subtractProductAndSum(n int) int {
96+
s, p := 0, 1
97+
for n != 0 {
98+
t := n % 10
99+
n /= 10
100+
s += t
101+
p *= t
102+
}
103+
return p - s
104+
}
53105
```
54106

55107
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int subtractProductAndSum(int n) {
4+
int s = 0, p = 1;
5+
while (n) {
6+
int t = n % 10;
7+
n /= 10;
8+
s += t;
9+
p *= t;
10+
}
11+
return p - s;
12+
}
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func subtractProductAndSum(n int) int {
2+
s, p := 0, 1
3+
for n != 0 {
4+
t := n % 10
5+
n /= 10
6+
s += t
7+
p *= t
8+
}
9+
return p - s
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int subtractProductAndSum(int n) {
3+
int s = 0, p = 1;
4+
while (n != 0) {
5+
int t = n % 10;
6+
n /= 10;
7+
s += t;
8+
p *= t;
9+
}
10+
return p - s;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def subtractProductAndSum(self, n: int) -> int:
3+
s, p = 0, 1
4+
while n:
5+
t = n % 10
6+
n //= 10
7+
s += t
8+
p *= t
9+
return p - s

0 commit comments

Comments
 (0)