-
Notifications
You must be signed in to change notification settings - Fork 481
/
0930.cpp
50 lines (49 loc) · 1.15 KB
/
0930.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int numSubarraysWithSum(vector<int>& A, int S)
{
vector<int> zeros;
int result = 0, cnt = 0;
if (A.empty()) return result;
for (auto num : A)
{
if (num)
{
zeros.push_back(cnt); cnt = 0;
}
else cnt++;
}
zeros.push_back(cnt);
if (S)
{
for (unsigned int i = 0; i < zeros.size() - S; ++i)
{
result += (zeros[i] + 1) * (zeros[i + S] + 1);
}
return result;
}
if (S == 0)
{
for (unsigned int i = 0; i < zeros.size(); ++i)
{
if (zeros[i]) result += (zeros[i] + 1)*zeros[i];
}
result /= 2;
return result;
}
}
};
int main()
{
vector<int> nums = {1, 1, 1, 1, 1};
int k = 3;
cout << Solution().numSubarraysWithSum(nums, k);
return 0;
}