Skip to content

Commit 2ec0d28

Browse files
authored
feat: add solutions to lc problem: No.2269 (#2555)
No.2269.Find the K-Beauty of a Number
1 parent a2432d4 commit 2ec0d28

File tree

7 files changed

+345
-2
lines changed

7 files changed

+345
-2
lines changed

solution/2200-2299/2269.Find the K-Beauty of a Number/README.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@
6464

6565
## 解法
6666

67-
### 方法一
67+
### 方法一:枚举
68+
69+
我们可以将 $num$ 转换为字符串 $s$,然后枚举 $s$ 的所有长度为 $k$ 的子字符串,将其转换为整数 $t$,判断 $t$ 是否能整除 $num$,如果能则答案加一。
70+
71+
时间复杂度 $O(\log num \times k)$,空间复杂度 $O(\log num + k)$。
6872

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

@@ -141,4 +145,124 @@ function divisorSubstrings(num: number, k: number): number {
141145

142146
<!-- tabs:end -->
143147

148+
### 方法二:滑动窗口
149+
150+
我们可以维护一个长度为 $k$ 的滑动窗口,初始时窗口中包含 $num$ 的最低 $k$ 位数字,然后每次向右移动一位,更新窗口中的数字,判断窗口中的数字是否能整除 $num$,如果能则答案加一。
151+
152+
时间复杂度 $O(\log num)$,空间复杂度 $O(1)$。
153+
154+
<!-- tabs:start -->
155+
156+
```python
157+
class Solution:
158+
def divisorSubstrings(self, num: int, k: int) -> int:
159+
x, p = 0, 1
160+
t = num
161+
for _ in range(k):
162+
t, v = divmod(t, 10)
163+
x = p * v + x
164+
p *= 10
165+
ans = int(x != 0 and num % x == 0)
166+
p //= 10
167+
while t:
168+
x //= 10
169+
t, v = divmod(t, 10)
170+
x = p * v + x
171+
ans += int(x != 0 and num % x == 0)
172+
return ans
173+
```
174+
175+
```java
176+
class Solution {
177+
public int divisorSubstrings(int num, int k) {
178+
int x = 0, p = 1;
179+
int t = num;
180+
for (; k > 0; --k) {
181+
int v = t % 10;
182+
t /= 10;
183+
x = p * v + x;
184+
p *= 10;
185+
}
186+
int ans = x != 0 && num % x == 0 ? 1 : 0;
187+
for (p /= 10; t > 0; t /= 10) {
188+
x /= 10;
189+
int v = t % 10;
190+
x = p * v + x;
191+
ans += (x != 0 && num % x == 0 ? 1 : 0);
192+
}
193+
return ans;
194+
}
195+
}
196+
```
197+
198+
```cpp
199+
class Solution {
200+
public:
201+
int divisorSubstrings(int num, int k) {
202+
int x = 0;
203+
long long p = 1;
204+
int t = num;
205+
for (; k > 0; --k) {
206+
int v = t % 10;
207+
t /= 10;
208+
x = p * v + x;
209+
p *= 10;
210+
}
211+
int ans = x != 0 && num % x == 0 ? 1 : 0;
212+
for (p /= 10; t > 0; t /= 10) {
213+
x /= 10;
214+
int v = t % 10;
215+
x = p * v + x;
216+
ans += (x != 0 && num % x == 0 ? 1 : 0);
217+
}
218+
return ans;
219+
}
220+
};
221+
```
222+
223+
```go
224+
func divisorSubstrings(num int, k int) (ans int) {
225+
x, p, t := 0, 1, num
226+
for ; k > 0; k-- {
227+
v := t % 10
228+
t /= 10
229+
x = p*v + x
230+
p *= 10
231+
}
232+
if x != 0 && num%x == 0 {
233+
ans++
234+
}
235+
for p /= 10; t > 0; t /= 10 {
236+
x /= 10
237+
v := t % 10
238+
x = p*v + x
239+
if x != 0 && num%x == 0 {
240+
ans++
241+
}
242+
}
243+
return
244+
}
245+
```
246+
247+
```ts
248+
function divisorSubstrings(num: number, k: number): number {
249+
let [x, p, t] = [0, 1, num];
250+
for (; k > 0; k--) {
251+
const v = t % 10;
252+
t = Math.floor(t / 10);
253+
x = p * v + x;
254+
p *= 10;
255+
}
256+
let ans = x !== 0 && num % x === 0 ? 1 : 0;
257+
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
258+
x = Math.floor(x / 10);
259+
x = p * (t % 10) + x;
260+
ans += x !== 0 && num % x === 0 ? 1 : 0;
261+
}
262+
return ans;
263+
}
264+
```
265+
266+
<!-- tabs:end -->
267+
144268
<!-- end -->

solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ Therefore, the k-beauty is 2.
6060

6161
## Solutions
6262

63-
### Solution 1
63+
### Solution 1: Enumeration
64+
65+
We can convert $num$ to a string $s$, then enumerate all substrings of $s$ with length $k$, convert them to an integer $t$, and check if $t$ is divisible by $num$. If it is, we increment the answer.
66+
67+
The time complexity is $O(\log num \times k)$, and the space complexity is $O(\log num + k)$.
6468

6569
<!-- tabs:start -->
6670

@@ -137,4 +141,124 @@ function divisorSubstrings(num: number, k: number): number {
137141

138142
<!-- tabs:end -->
139143

144+
### Solution 2: Sliding Window
145+
146+
We can maintain a sliding window of length $k$. Initially, the window contains the lowest $k$ digits of $num$. Then, for each iteration, we move the window one digit to the right, update the number in the window, and check if the number in the window is divisible by $num$. If it is, we increment the answer.
147+
148+
The time complexity is $O(\log num)$, and the space complexity is $O(1)$.
149+
150+
<!-- tabs:start -->
151+
152+
```python
153+
class Solution:
154+
def divisorSubstrings(self, num: int, k: int) -> int:
155+
x, p = 0, 1
156+
t = num
157+
for _ in range(k):
158+
t, v = divmod(t, 10)
159+
x = p * v + x
160+
p *= 10
161+
ans = int(x != 0 and num % x == 0)
162+
p //= 10
163+
while t:
164+
x //= 10
165+
t, v = divmod(t, 10)
166+
x = p * v + x
167+
ans += int(x != 0 and num % x == 0)
168+
return ans
169+
```
170+
171+
```java
172+
class Solution {
173+
public int divisorSubstrings(int num, int k) {
174+
int x = 0, p = 1;
175+
int t = num;
176+
for (; k > 0; --k) {
177+
int v = t % 10;
178+
t /= 10;
179+
x = p * v + x;
180+
p *= 10;
181+
}
182+
int ans = x != 0 && num % x == 0 ? 1 : 0;
183+
for (p /= 10; t > 0; t /= 10) {
184+
x /= 10;
185+
int v = t % 10;
186+
x = p * v + x;
187+
ans += (x != 0 && num % x == 0 ? 1 : 0);
188+
}
189+
return ans;
190+
}
191+
}
192+
```
193+
194+
```cpp
195+
class Solution {
196+
public:
197+
int divisorSubstrings(int num, int k) {
198+
int x = 0;
199+
long long p = 1;
200+
int t = num;
201+
for (; k > 0; --k) {
202+
int v = t % 10;
203+
t /= 10;
204+
x = p * v + x;
205+
p *= 10;
206+
}
207+
int ans = x != 0 && num % x == 0 ? 1 : 0;
208+
for (p /= 10; t > 0; t /= 10) {
209+
x /= 10;
210+
int v = t % 10;
211+
x = p * v + x;
212+
ans += (x != 0 && num % x == 0 ? 1 : 0);
213+
}
214+
return ans;
215+
}
216+
};
217+
```
218+
219+
```go
220+
func divisorSubstrings(num int, k int) (ans int) {
221+
x, p, t := 0, 1, num
222+
for ; k > 0; k-- {
223+
v := t % 10
224+
t /= 10
225+
x = p*v + x
226+
p *= 10
227+
}
228+
if x != 0 && num%x == 0 {
229+
ans++
230+
}
231+
for p /= 10; t > 0; t /= 10 {
232+
x /= 10
233+
v := t % 10
234+
x = p*v + x
235+
if x != 0 && num%x == 0 {
236+
ans++
237+
}
238+
}
239+
return
240+
}
241+
```
242+
243+
```ts
244+
function divisorSubstrings(num: number, k: number): number {
245+
let [x, p, t] = [0, 1, num];
246+
for (; k > 0; k--) {
247+
const v = t % 10;
248+
t = Math.floor(t / 10);
249+
x = p * v + x;
250+
p *= 10;
251+
}
252+
let ans = x !== 0 && num % x === 0 ? 1 : 0;
253+
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
254+
x = Math.floor(x / 10);
255+
x = p * (t % 10) + x;
256+
ans += x !== 0 && num % x === 0 ? 1 : 0;
257+
}
258+
return ans;
259+
}
260+
```
261+
262+
<!-- tabs:end -->
263+
140264
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int divisorSubstrings(int num, int k) {
4+
int x = 0;
5+
long long p = 1;
6+
int t = num;
7+
for (; k > 0; --k) {
8+
int v = t % 10;
9+
t /= 10;
10+
x = p * v + x;
11+
p *= 10;
12+
}
13+
int ans = x != 0 && num % x == 0 ? 1 : 0;
14+
for (p /= 10; t > 0; t /= 10) {
15+
x /= 10;
16+
int v = t % 10;
17+
x = p * v + x;
18+
ans += (x != 0 && num % x == 0 ? 1 : 0);
19+
}
20+
return ans;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func divisorSubstrings(num int, k int) (ans int) {
2+
x, p, t := 0, 1, num
3+
for ; k > 0; k-- {
4+
v := t % 10
5+
t /= 10
6+
x = p*v + x
7+
p *= 10
8+
}
9+
if x != 0 && num%x == 0 {
10+
ans++
11+
}
12+
for p /= 10; t > 0; t /= 10 {
13+
x /= 10
14+
v := t % 10
15+
x = p*v + x
16+
if x != 0 && num%x == 0 {
17+
ans++
18+
}
19+
}
20+
return
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int divisorSubstrings(int num, int k) {
3+
int x = 0, p = 1;
4+
int t = num;
5+
for (; k > 0; --k) {
6+
int v = t % 10;
7+
t /= 10;
8+
x = p * v + x;
9+
p *= 10;
10+
}
11+
int ans = x != 0 && num % x == 0 ? 1 : 0;
12+
for (p /= 10; t > 0; t /= 10) {
13+
x /= 10;
14+
int v = t % 10;
15+
x = p * v + x;
16+
ans += (x != 0 && num % x == 0 ? 1 : 0);
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def divisorSubstrings(self, num: int, k: int) -> int:
3+
x, p = 0, 1
4+
t = num
5+
for _ in range(k):
6+
t, v = divmod(t, 10)
7+
x = p * v + x
8+
p *= 10
9+
ans = int(x != 0 and num % x == 0)
10+
p //= 10
11+
while t:
12+
x //= 10
13+
t, v = divmod(t, 10)
14+
x = p * v + x
15+
ans += int(x != 0 and num % x == 0)
16+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function divisorSubstrings(num: number, k: number): number {
2+
let [x, p, t] = [0, 1, num];
3+
for (; k > 0; k--) {
4+
const v = t % 10;
5+
t = Math.floor(t / 10);
6+
x = p * v + x;
7+
p *= 10;
8+
}
9+
let ans = x !== 0 && num % x === 0 ? 1 : 0;
10+
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
11+
x = Math.floor(x / 10);
12+
x = p * (t % 10) + x;
13+
ans += x !== 0 && num % x === 0 ? 1 : 0;
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)