-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2269 (#2555)
No.2269.Find the K-Beauty of a Number
- Loading branch information
Showing
7 changed files
with
345 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
20 changes: 20 additions & 0 deletions
20
solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
solution/2200-2299/2269.Find the K-Beauty of a Number/Solution2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |