Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2269 (#2555)
Browse files Browse the repository at this point in the history
No.2269.Find the K-Beauty of a Number
  • Loading branch information
yanglbme authored Apr 8, 2024
1 parent a2432d4 commit 2ec0d28
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 2 deletions.
126 changes: 125 additions & 1 deletion solution/2200-2299/2269.Find the K-Beauty of a Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@

## 解法

### 方法一
### 方法一:枚举

我们可以将 $num$ 转换为字符串 $s$,然后枚举 $s$ 的所有长度为 $k$ 的子字符串,将其转换为整数 $t$,判断 $t$ 是否能整除 $num$,如果能则答案加一。

时间复杂度 $O(\log num \times k)$,空间复杂度 $O(\log num + k)$。

<!-- tabs:start -->

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

<!-- tabs:end -->

### 方法二:滑动窗口

我们可以维护一个长度为 $k$ 的滑动窗口,初始时窗口中包含 $num$ 的最低 $k$ 位数字,然后每次向右移动一位,更新窗口中的数字,判断窗口中的数字是否能整除 $num$,如果能则答案加一。

时间复杂度 $O(\log num)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
x, p = 0, 1
t = num
for _ in range(k):
t, v = divmod(t, 10)
x = p * v + x
p *= 10
ans = int(x != 0 and num % x == 0)
p //= 10
while t:
x //= 10
t, v = divmod(t, 10)
x = p * v + x
ans += int(x != 0 and num % x == 0)
return ans
```

```java
class Solution {
public int divisorSubstrings(int num, int k) {
int x = 0, p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
}
```

```cpp
class Solution {
public:
int divisorSubstrings(int num, int k) {
int x = 0;
long long p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
};
```
```go
func divisorSubstrings(num int, k int) (ans int) {
x, p, t := 0, 1, num
for ; k > 0; k-- {
v := t % 10
t /= 10
x = p*v + x
p *= 10
}
if x != 0 && num%x == 0 {
ans++
}
for p /= 10; t > 0; t /= 10 {
x /= 10
v := t % 10
x = p*v + x
if x != 0 && num%x == 0 {
ans++
}
}
return
}
```

```ts
function divisorSubstrings(num: number, k: number): number {
let [x, p, t] = [0, 1, num];
for (; k > 0; k--) {
const v = t % 10;
t = Math.floor(t / 10);
x = p * v + x;
p *= 10;
}
let ans = x !== 0 && num % x === 0 ? 1 : 0;
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
x = Math.floor(x / 10);
x = p * (t % 10) + x;
ans += x !== 0 && num % x === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
126 changes: 125 additions & 1 deletion solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ Therefore, the k-beauty is 2.

## Solutions

### Solution 1
### Solution 1: Enumeration

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.

The time complexity is $O(\log num \times k)$, and the space complexity is $O(\log num + k)$.

<!-- tabs:start -->

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

<!-- tabs:end -->

### Solution 2: Sliding Window

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.

The time complexity is $O(\log num)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
x, p = 0, 1
t = num
for _ in range(k):
t, v = divmod(t, 10)
x = p * v + x
p *= 10
ans = int(x != 0 and num % x == 0)
p //= 10
while t:
x //= 10
t, v = divmod(t, 10)
x = p * v + x
ans += int(x != 0 and num % x == 0)
return ans
```

```java
class Solution {
public int divisorSubstrings(int num, int k) {
int x = 0, p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
}
```

```cpp
class Solution {
public:
int divisorSubstrings(int num, int k) {
int x = 0;
long long p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
};
```
```go
func divisorSubstrings(num int, k int) (ans int) {
x, p, t := 0, 1, num
for ; k > 0; k-- {
v := t % 10
t /= 10
x = p*v + x
p *= 10
}
if x != 0 && num%x == 0 {
ans++
}
for p /= 10; t > 0; t /= 10 {
x /= 10
v := t % 10
x = p*v + x
if x != 0 && num%x == 0 {
ans++
}
}
return
}
```

```ts
function divisorSubstrings(num: number, k: number): number {
let [x, p, t] = [0, 1, num];
for (; k > 0; k--) {
const v = t % 10;
t = Math.floor(t / 10);
x = p * v + x;
p *= 10;
}
let ans = x !== 0 && num % x === 0 ? 1 : 0;
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
x = Math.floor(x / 10);
x = p * (t % 10) + x;
ans += x !== 0 && num % x === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int divisorSubstrings(int num, int k) {
int x = 0;
long long p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
};
21 changes: 21 additions & 0 deletions solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func divisorSubstrings(num int, k int) (ans int) {
x, p, t := 0, 1, num
for ; k > 0; k-- {
v := t % 10
t /= 10
x = p*v + x
p *= 10
}
if x != 0 && num%x == 0 {
ans++
}
for p /= 10; t > 0; t /= 10 {
x /= 10
v := t % 10
x = p*v + x
if x != 0 && num%x == 0 {
ans++
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int divisorSubstrings(int num, int k) {
int x = 0, p = 1;
int t = num;
for (; k > 0; --k) {
int v = t % 10;
t /= 10;
x = p * v + x;
p *= 10;
}
int ans = x != 0 && num % x == 0 ? 1 : 0;
for (p /= 10; t > 0; t /= 10) {
x /= 10;
int v = t % 10;
x = p * v + x;
ans += (x != 0 && num % x == 0 ? 1 : 0);
}
return ans;
}
}
16 changes: 16 additions & 0 deletions solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
x, p = 0, 1
t = num
for _ in range(k):
t, v = divmod(t, 10)
x = p * v + x
p *= 10
ans = int(x != 0 and num % x == 0)
p //= 10
while t:
x //= 10
t, v = divmod(t, 10)
x = p * v + x
ans += int(x != 0 and num % x == 0)
return ans
16 changes: 16 additions & 0 deletions solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function divisorSubstrings(num: number, k: number): number {
let [x, p, t] = [0, 1, num];
for (; k > 0; k--) {
const v = t % 10;
t = Math.floor(t / 10);
x = p * v + x;
p *= 10;
}
let ans = x !== 0 && num % x === 0 ? 1 : 0;
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
x = Math.floor(x / 10);
x = p * (t % 10) + x;
ans += x !== 0 && num % x === 0 ? 1 : 0;
}
return ans;
}

0 comments on commit 2ec0d28

Please sign in to comment.