Skip to content

Commit 3d51b11

Browse files
committed
728.Self Dividing Numbers.
1 parent 283b7d4 commit 3d51b11

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
A self-dividing number is a number that is divisible by every digit it contains.
3+
4+
For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
5+
A self-dividing number is not allowed to contain the digit zero.
6+
7+
Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].
8+
9+
10+
11+
Example 1:
12+
Input: left = 1, right = 22
13+
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
14+
15+
Example 2:
16+
Input: left = 47, right = 85
17+
Output: [48,55,66,77]
18+
19+
20+
Constraints:
21+
- 1 <= left <= right <= 10^4
22+
*/
23+
class Solution {
24+
func selfDividingNumbers(_ left: Int, _ right: Int) -> [Int] {
25+
var result = [Int]()
26+
for i in left...right {
27+
let element = String(i)
28+
for (j, c) in element.enumerated() {
29+
if c.wholeNumberValue! == 0 {
30+
break
31+
}
32+
if i % c.wholeNumberValue! != 0 {
33+
break
34+
}
35+
if j == element.count - 1 {
36+
result.append(i)
37+
}
38+
}
39+
}
40+
return result
41+
}
42+
}
43+
44+
let s = Solution()
45+
let r = s.selfDividingNumbers(1, 22)
46+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/728.Self Dividing Numbers.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
126. [Binary Search](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/704.Binary%20Search.playground/Contents.swift)
131131
127. [To Lower Case](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/709.To%20Lower%20Case.playground/Contents.swift)
132132
128. [1-bit and 2-bit Characters](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/717.1-bit%20and%202-bit%20Characters.playground/Contents.swift)
133+
129. [Self Dividing Numbers](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/728.Self%20Dividing%20Numbers.playground/Contents.swift)
133134

134135
#### Medium
135136

0 commit comments

Comments
 (0)