Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you use a balanced binary search tree, you can get O(nlgn) - like std::set<int>
class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
size_t len = A.size(); vector<int> presum(len + );
std::partial_sum(A.begin(), A.end(), presum.begin() + ); int ret = ;
for(int i = ; i <= len; i ++)
{
for(int j = ; j < i; j++)
{
int diff = presum[i] - presum[j];
if(diff<=end && diff>=start)
ret ++;
}
}
return ret;
}
};