Skip to content

Commit dcaf488

Browse files
committed
feat: add solutions to lc problem: No.3622
No.3622.Check Divisibility by Digit Sum and Product
1 parent 2604b21 commit dcaf488

File tree

7 files changed

+192
-8
lines changed

7 files changed

+192
-8
lines changed

solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3622.Ch
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:模拟
71+
72+
我们可以遍历整数 $n$ 的每一位数字,计算出数字和 $s$ 和数字积 $p$。最后判断 $n$ 是否能被 $s + p$ 整除。
73+
74+
时间复杂度 $O(\log n)$,其中 $n$ 为整数 $n$ 的值。空间复杂度 $O(1)$。
7175

7276
<!-- tabs:start -->
7377

7478
#### Python3
7579

7680
```python
77-
81+
class Solution:
82+
def checkDivisibility(self, n: int) -> bool:
83+
s, p = 0, 1
84+
x = n
85+
while x:
86+
x, v = divmod(x, 10)
87+
s += v
88+
p *= v
89+
return n % (s + p) == 0
7890
```
7991

8092
#### Java
8193

8294
```java
83-
95+
class Solution {
96+
public boolean checkDivisibility(int n) {
97+
int s = 0, p = 1;
98+
int x = n;
99+
while (x != 0) {
100+
int v = x % 10;
101+
x /= 10;
102+
s += v;
103+
p *= v;
104+
}
105+
return n % (s + p) == 0;
106+
}
107+
}
84108
```
85109

86110
#### C++
87111

88112
```cpp
89-
113+
class Solution {
114+
public:
115+
bool checkDivisibility(int n) {
116+
int s = 0, p = 1;
117+
int x = n;
118+
while (x != 0) {
119+
int v = x % 10;
120+
x /= 10;
121+
s += v;
122+
p *= v;
123+
}
124+
return n % (s + p) == 0;
125+
}
126+
};
90127
```
91128
92129
#### Go
93130
94131
```go
132+
func checkDivisibility(n int) bool {
133+
s, p := 0, 1
134+
x := n
135+
for x != 0 {
136+
v := x % 10
137+
x /= 10
138+
s += v
139+
p *= v
140+
}
141+
return n%(s+p) == 0
142+
}
143+
```
95144

145+
#### TypeScript
146+
147+
```ts
148+
function checkDivisibility(n: number): boolean {
149+
let [s, p] = [0, 1];
150+
let x = n;
151+
while (x !== 0) {
152+
const v = x % 10;
153+
x = Math.floor(x / 10);
154+
s += v;
155+
p *= v;
156+
}
157+
return n % (s + p) === 0;
158+
}
96159
```
97160

98161
<!-- tabs:end -->

solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README_EN.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3622.Ch
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Simulation
69+
70+
We can iterate through each digit of the integer $n$, calculating the digit sum $s$ and digit product $p$. Finally, we check whether $n$ is divisible by $s + p$.
71+
72+
The time complexity is $O(\log n)$, where $n$ is the value of the integer $n$. The space complexity is $O(1)$.
6973

7074
<!-- tabs:start -->
7175

7276
#### Python3
7377

7478
```python
75-
79+
class Solution:
80+
def checkDivisibility(self, n: int) -> bool:
81+
s, p = 0, 1
82+
x = n
83+
while x:
84+
x, v = divmod(x, 10)
85+
s += v
86+
p *= v
87+
return n % (s + p) == 0
7688
```
7789

7890
#### Java
7991

8092
```java
81-
93+
class Solution {
94+
public boolean checkDivisibility(int n) {
95+
int s = 0, p = 1;
96+
int x = n;
97+
while (x != 0) {
98+
int v = x % 10;
99+
x /= 10;
100+
s += v;
101+
p *= v;
102+
}
103+
return n % (s + p) == 0;
104+
}
105+
}
82106
```
83107

84108
#### C++
85109

86110
```cpp
87-
111+
class Solution {
112+
public:
113+
bool checkDivisibility(int n) {
114+
int s = 0, p = 1;
115+
int x = n;
116+
while (x != 0) {
117+
int v = x % 10;
118+
x /= 10;
119+
s += v;
120+
p *= v;
121+
}
122+
return n % (s + p) == 0;
123+
}
124+
};
88125
```
89126
90127
#### Go
91128
92129
```go
130+
func checkDivisibility(n int) bool {
131+
s, p := 0, 1
132+
x := n
133+
for x != 0 {
134+
v := x % 10
135+
x /= 10
136+
s += v
137+
p *= v
138+
}
139+
return n%(s+p) == 0
140+
}
141+
```
93142

143+
#### TypeScript
144+
145+
```ts
146+
function checkDivisibility(n: number): boolean {
147+
let [s, p] = [0, 1];
148+
let x = n;
149+
while (x !== 0) {
150+
const v = x % 10;
151+
x = Math.floor(x / 10);
152+
s += v;
153+
p *= v;
154+
}
155+
return n % (s + p) === 0;
156+
}
94157
```
95158

96159
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool checkDivisibility(int n) {
4+
int s = 0, p = 1;
5+
int x = n;
6+
while (x != 0) {
7+
int v = x % 10;
8+
x /= 10;
9+
s += v;
10+
p *= v;
11+
}
12+
return n % (s + p) == 0;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func checkDivisibility(n int) bool {
2+
s, p := 0, 1
3+
x := n
4+
for x != 0 {
5+
v := x % 10
6+
x /= 10
7+
s += v
8+
p *= v
9+
}
10+
return n%(s+p) == 0
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean checkDivisibility(int n) {
3+
int s = 0, p = 1;
4+
int x = n;
5+
while (x != 0) {
6+
int v = x % 10;
7+
x /= 10;
8+
s += v;
9+
p *= v;
10+
}
11+
return n % (s + p) == 0;
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def checkDivisibility(self, n: int) -> bool:
3+
s, p = 0, 1
4+
x = n
5+
while x:
6+
x, v = divmod(x, 10)
7+
s += v
8+
p *= v
9+
return n % (s + p) == 0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function checkDivisibility(n: number): boolean {
2+
let [s, p] = [0, 1];
3+
let x = n;
4+
while (x !== 0) {
5+
const v = x % 10;
6+
x = Math.floor(x / 10);
7+
s += v;
8+
p *= v;
9+
}
10+
return n % (s + p) === 0;
11+
}

0 commit comments

Comments
 (0)