File tree 6 files changed +204
-35
lines changed
solution/0700-0799/0728.Self Dividing Numbers
6 files changed +204
-35
lines changed Original file line number Diff line number Diff line change 51
51
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
52
53
53
``` python
54
-
54
+ class Solution :
55
+ def selfDividingNumbers (self , left : int , right : int ) -> List[int ]:
56
+ return [num for num in range (left, right + 1 ) if all (i != ' 0' and num % int (i) == 0 for i in str (num))]
55
57
```
56
58
57
59
### ** Java**
58
60
59
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
62
61
63
``` java
64
+ class Solution {
65
+ public List<Integer > selfDividingNumbers (int left , int right ) {
66
+ List<Integer > ans = new ArrayList<> ();
67
+ for (int i = left; i <= right; ++ i) {
68
+ if (check(i)) {
69
+ ans. add(i);
70
+ }
71
+ }
72
+ return ans;
73
+ }
62
74
75
+ private boolean check (int num ) {
76
+ for (int t = num; t != 0 ; t /= 10 ) {
77
+ int x = t % 10 ;
78
+ if (x == 0 || num % x != 0 ) {
79
+ return false ;
80
+ }
81
+ }
82
+ return true ;
83
+ }
84
+ }
63
85
```
64
86
65
87
### ** Rust**
@@ -88,6 +110,54 @@ impl Solution {
88
110
}
89
111
```
90
112
113
+ ### ** C++**
114
+
115
+ ``` cpp
116
+ class Solution {
117
+ public:
118
+ vector<int > selfDividingNumbers(int left, int right) {
119
+ vector<int > ans;
120
+ for (int i = left; i <= right; ++i)
121
+ if (check(i))
122
+ ans.push_back(i);
123
+ return ans;
124
+ }
125
+
126
+ bool check(int num) {
127
+ for (int t = num; t; t /= 10)
128
+ {
129
+ int x = t % 10;
130
+ if (x == 0 || num % x) return false;
131
+ }
132
+ return true ;
133
+ }
134
+ };
135
+ ```
136
+
137
+ ### ** Go**
138
+
139
+ ``` go
140
+ func selfDividingNumbers (left int , right int ) []int {
141
+ check := func (num int ) bool {
142
+ for t := num; t != 0 ; t /= 10 {
143
+ x := t % 10
144
+ if x == 0 || num%x != 0 {
145
+ return false
146
+ }
147
+ }
148
+ return true
149
+ }
150
+
151
+ var ans []int
152
+ for i := left; i <= right; i++ {
153
+ if check (i) {
154
+ ans = append (ans, i)
155
+ }
156
+ }
157
+ return ans
158
+ }
159
+ ```
160
+
91
161
### ** ...**
92
162
93
163
```
Original file line number Diff line number Diff line change 36
36
### ** Python3**
37
37
38
38
``` python
39
-
39
+ class Solution :
40
+ def selfDividingNumbers (self , left : int , right : int ) -> List[int ]:
41
+ return [num for num in range (left, right + 1 ) if all (i != ' 0' and num % int (i) == 0 for i in str (num))]
40
42
```
41
43
42
44
### ** Java**
43
45
44
46
``` java
47
+ class Solution {
48
+ public List<Integer > selfDividingNumbers (int left , int right ) {
49
+ List<Integer > ans = new ArrayList<> ();
50
+ for (int i = left; i <= right; ++ i) {
51
+ if (check(i)) {
52
+ ans. add(i);
53
+ }
54
+ }
55
+ return ans;
56
+ }
45
57
58
+ private boolean check (int num ) {
59
+ for (int t = num; t != 0 ; t /= 10 ) {
60
+ int x = t % 10 ;
61
+ if (x == 0 || num % x != 0 ) {
62
+ return false ;
63
+ }
64
+ }
65
+ return true ;
66
+ }
67
+ }
46
68
```
47
69
48
70
### ** Rust**
@@ -71,6 +93,54 @@ impl Solution {
71
93
}
72
94
```
73
95
96
+ ### ** C++**
97
+
98
+ ``` cpp
99
+ class Solution {
100
+ public:
101
+ vector<int > selfDividingNumbers(int left, int right) {
102
+ vector<int > ans;
103
+ for (int i = left; i <= right; ++i)
104
+ if (check(i))
105
+ ans.push_back(i);
106
+ return ans;
107
+ }
108
+
109
+ bool check(int num) {
110
+ for (int t = num; t; t /= 10)
111
+ {
112
+ int x = t % 10;
113
+ if (x == 0 || num % x) return false;
114
+ }
115
+ return true ;
116
+ }
117
+ };
118
+ ```
119
+
120
+ ### ** Go**
121
+
122
+ ``` go
123
+ func selfDividingNumbers (left int , right int ) []int {
124
+ check := func (num int ) bool {
125
+ for t := num; t != 0 ; t /= 10 {
126
+ x := t % 10
127
+ if x == 0 || num%x != 0 {
128
+ return false
129
+ }
130
+ }
131
+ return true
132
+ }
133
+
134
+ var ans []int
135
+ for i := left; i <= right; i++ {
136
+ if check (i) {
137
+ ans = append (ans, i)
138
+ }
139
+ }
140
+ return ans
141
+ }
142
+ ```
143
+
74
144
### ** ...**
75
145
76
146
```
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public:
3
- int div (int num){
4
-
5
- int temp = num, r;
6
-
7
- while ( temp > 0 ){
8
-
9
- r = temp % 10 ;
10
-
11
- if ( r == 0 || num % r != 0 ){
12
- return 0 ;
13
- }
14
-
15
- temp /= 10 ;
16
- }
17
-
18
- return 1 ;
19
- }
20
-
21
- vector<int > selfDividingNumbers (int left, int right) {
22
-
23
- vector<int > ret;
24
-
25
- for (int i = left; i <= right; i++){
26
- if ( div (i) ){
27
- ret.push_back (i);
28
- }
29
- }
30
-
31
- return ret;
32
- }
33
- };
1
+ class Solution {
2
+ public:
3
+ vector<int > selfDividingNumbers (int left, int right) {
4
+ vector<int > ans;
5
+ for (int i = left; i <= right; ++i)
6
+ if (check (i))
7
+ ans.push_back (i);
8
+ return ans;
9
+ }
10
+
11
+ bool check (int num) {
12
+ for (int t = num; t; t /= 10 )
13
+ {
14
+ int x = t % 10 ;
15
+ if (x == 0 || num % x) return false ;
16
+ }
17
+ return true ;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func selfDividingNumbers (left int , right int ) []int {
2
+ check := func (num int ) bool {
3
+ for t := num ; t != 0 ; t /= 10 {
4
+ x := t % 10
5
+ if x == 0 || num % x != 0 {
6
+ return false
7
+ }
8
+ }
9
+ return true
10
+ }
11
+
12
+ var ans []int
13
+ for i := left ; i <= right ; i ++ {
14
+ if check (i ) {
15
+ ans = append (ans , i )
16
+ }
17
+ }
18
+ return ans
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > selfDividingNumbers (int left , int right ) {
3
+ List <Integer > ans = new ArrayList <>();
4
+ for (int i = left ; i <= right ; ++i ) {
5
+ if (check (i )) {
6
+ ans .add (i );
7
+ }
8
+ }
9
+ return ans ;
10
+ }
11
+
12
+ private boolean check (int num ) {
13
+ for (int t = num ; t != 0 ; t /= 10 ) {
14
+ int x = t % 10 ;
15
+ if (x == 0 || num % x != 0 ) {
16
+ return false ;
17
+ }
18
+ }
19
+ return true ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def selfDividingNumbers (self , left : int , right : int ) -> List [int ]:
3
+ return [num for num in range (left , right + 1 ) if all (i != '0' and num % int (i ) == 0 for i in str (num ))]
You can’t perform that action at this time.
0 commit comments