题目描述:

image-20250310184718308

解析:滑动窗口,维护一个长度为k的窗口,如果能整除num;ans+1

package March;

/**
* @author hxw
* @version 1.0
* @date 2025/3/10 18:12
* @description: 2269. 找到一个数字的 K 美丽值 简单
*/
public class ten {
public static void main(String[] args) {
Solution solution = new Solution();
int result = solution.divisorSubstrings(240, 2);
System.out.println(result);
}

static 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;
}
}
}