Skip to content

Commit 15a5a40

Browse files
[N-0] add 728
1 parent 23e3800 commit 15a5a40

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|728|[Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_728.java) | O(n*k) k is the average number of digits of each number in the given array| O(1) | Easy |
2526
|727|[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_727.java) | O(m*n) | O(m*n) | Hard | DP
2627
|725|[Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_725.java) | O(n+k) | O(k) | Medium | LinkedList
2728
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_724.java) | O(n) | O(1) | Easy | Array
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 728. Self Dividing Numbers
8+
*
9+
* A self-dividing number is a number that is divisible by every digit it contains.
10+
* For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
11+
* Also, a self-dividing number is not allowed to contain the digit zero.
12+
* Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
13+
14+
Example 1:
15+
Input:
16+
left = 1, right = 22
17+
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
18+
Note:
19+
20+
The boundaries of each input argument are 1 <= left <= right <= 10000.
21+
*/
22+
public class _728 {
23+
public static class Solution1 {
24+
public List<Integer> selfDividingNumbers(int left, int right) {
25+
List<Integer> result = new ArrayList<>();
26+
for (int num = left; num <= right; num++) {
27+
if (isSelfDividing(num)) {
28+
result.add(num);
29+
}
30+
}
31+
return result;
32+
}
33+
34+
private boolean isSelfDividing(int num) {
35+
int tmp = num;
36+
while (tmp != 0) {
37+
int digit = tmp % 10;
38+
if (digit == 0 || num % digit != 0) {
39+
return false;
40+
}
41+
tmp /= 10;
42+
}
43+
return true;
44+
}
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._728;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _728Test {
13+
private static _728.Solution1 solution1;
14+
private static List<Integer> expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _728.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22);
24+
assertEquals(expected, solution1.selfDividingNumbers(1, 22));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)