Skip to content

Commit 0c289e0

Browse files
add 1281
1 parent 84f0a35 commit 0c289e0

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
1010
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
1111
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Medium||
12+
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
1213
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Easy||
1314
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
1415
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1281. Subtract the Product and Sum of Digits of an Integer
5+
*
6+
* Given an integer number n, return the difference between the product of its digits and the sum of its digits.
7+
*
8+
* Example 1:
9+
* Input: n = 234
10+
* Output: 15
11+
* Explanation:
12+
* Product of digits = 2 * 3 * 4 = 24
13+
* Sum of digits = 2 + 3 + 4 = 9
14+
* Result = 24 - 9 = 15
15+
*
16+
* Example 2:
17+
* Input: n = 4421
18+
* Output: 21
19+
* Explanation:
20+
* Product of digits = 4 * 4 * 2 * 1 = 32
21+
* Sum of digits = 4 + 4 + 2 + 1 = 11
22+
* Result = 32 - 11 = 21
23+
*
24+
* Constraints:
25+
* 1 <= n <= 10^5
26+
* */
27+
public class _1281 {
28+
public static class Solution1 {
29+
public int subtractProductAndSum(int n) {
30+
int sum = 0;
31+
int product = 1;
32+
while (n > 0) {
33+
int digit = n % 10;
34+
n /= 10;
35+
sum += digit;
36+
product *= digit;
37+
}
38+
return product - sum;
39+
}
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1281;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1281Test {
10+
private static _1281.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1281.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(15, solution1.subtractProductAndSum(234));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)