| comments | true | |
|---|---|---|
| difficulty | 简单 | |
| edit_url | https://github.com/doocs/leetcode/edit/main/solution/2500-2599/2520.Count%20the%20Digits%20That%20Divide%20a%20Number/README.md | |
| rating | 1260 | |
| source | 第 326 场周赛 Q1 | |
| tags |
|
给你一个整数 num ,返回 num 中能整除 num 的数位的数目。
如果满足 nums % val == 0 ,则认为整数 val 可以整除 nums 。
示例 1:
输入:num = 7 输出:1 解释:7 被自己整除,因此答案是 1 。
示例 2:
输入:num = 121 输出:2 解释:121 可以被 1 整除,但无法被 2 整除。由于 1 出现两次,所以返回 2 。
示例 3:
输入:num = 1248 输出:4 解释:1248 可以被它每一位上的数字整除,因此答案是 4 。
提示:
1 <= num <= 109num的数位中不含0
我们直接枚举整数
枚举结束后,返回答案即可。
时间复杂度
class Solution:
def countDigits(self, num: int) -> int:
ans, x = 0, num
while x:
x, val = divmod(x, 10)
ans += num % val == 0
return ansclass Solution {
public int countDigits(int num) {
int ans = 0;
for (int x = num; x > 0; x /= 10) {
if (num % (x % 10) == 0) {
++ans;
}
}
return ans;
}
}class Solution {
public:
int countDigits(int num) {
int ans = 0;
for (int x = num; x > 0; x /= 10) {
if (num % (x % 10) == 0) {
++ans;
}
}
return ans;
}
};func countDigits(num int) (ans int) {
for x := num; x > 0; x /= 10 {
if num%(x%10) == 0 {
ans++
}
}
return
}function countDigits(num: number): number {
let ans = 0;
for (let x = num; x; x = (x / 10) | 0) {
if (num % (x % 10) === 0) {
++ans;
}
}
return ans;
}impl Solution {
pub fn count_digits(num: i32) -> i32 {
let mut ans = 0;
let mut cur = num;
while cur != 0 {
if num % (cur % 10) == 0 {
ans += 1;
}
cur /= 10;
}
ans
}
}int countDigits(int num) {
int ans = 0;
int cur = num;
while (cur) {
if (num % (cur % 10) == 0) {
ans++;
}
cur /= 10;
}
return ans;
}function countDigits(num: number): number {
let ans = 0;
for (const s of num.toString()) {
if (num % Number(s) === 0) {
ans++;
}
}
return ans;
}impl Solution {
pub fn count_digits(num: i32) -> i32 {
num.to_string()
.chars()
.filter(|&c| c != '0')
.filter(|&c| num % (c.to_digit(10).unwrap() as i32) == 0)
.count() as i32
}
}