-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3969.cpp
More file actions
30 lines (30 loc) · 816 Bytes
/
Copy path3969.cpp
File metadata and controls
30 lines (30 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
bool valid(long long sum , int x) {
int index = 0;
int digit = -1;
while (sum) {
digit = sum % 10;
if (index == 0 && digit != x) return false;
sum /= 10;
index++;
}
return digit == x;
}
int countValidSubarrays(vector<int>& nums, int x) {
int n = nums.size();
vector<long long> prefix(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
int res = 0;
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
// [i, j]
long long sum = prefix[j] - prefix[i - 1];
if (valid(sum, x)) res++;
}
}
return res;
}
};