solution.md 4.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 区间和的个数
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
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
<p>给你一个整数数组 <code>nums</code> 以及两个整数 <code>lower</code><code>upper</code> 。求数组中,值位于范围 <code>[lower, upper]</code> (包含 <code>lower</code> 和 <code>upper</code>)之内的 <strong>区间和的个数</strong></p>

<p><strong>区间和</strong> <code>S(i, j)</code> 表示在 <code>nums</code> 中,位置从 <code>i</code> 到 <code>j</code> 的元素之和,包含 <code>i</code> 和 <code>j</code> (<code>i</code><code>j</code>)。</p>

<p> </p>
<strong>示例 1:</strong>

<pre>
<strong>输入:</strong>nums = [-2,5,-1], lower = -2, upper = 2
<strong>输出:</strong>3
<strong>解释:</strong>存在三个区间:[0,0]、[2,2] 和 [0,2] ,对应的区间和分别是:-2 、-1 、2 。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [0], lower = 0, upper = 0
<strong>输出:</strong>1
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
	<li><code>-10<sup>5</sup> <= lower <= upper <= 10<sup>5</sup></code></li>
	<li>题目数据保证答案是一个 <strong>32 位</strong> 的整数</li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
34
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
35

每日一练社区's avatar
每日一练社区 已提交
36
## aop
F
fix bug  
feilong 已提交
37

每日一练社区's avatar
每日一练社区 已提交
38
### before
F
fix bug  
feilong 已提交
39

每日一练社区's avatar
每日一练社区 已提交
40 41 42 43 44
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
45

每日一练社区's avatar
每日一练社区 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
```cpp
int main()
{
    Solution sol;
    int res;
    vector<int> nums = {-2, 5, -1};
    int lower = -2;
    int upper = 2;
    res = sol.countRangeSum(nums, lower, upper);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
61

每日一练社区's avatar
每日一练社区 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
```cpp
class Solution
{
public:
    int lower, upper, res;
    long tmp[10000];
    void merge(vector<long> &a, int l, int mid, int r)
    {
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r)
        {
            if (a[i] <= a[j])
                tmp[k++] = a[i++];
            else
                tmp[k++] = a[j++];
        }
        while (i <= mid)
            tmp[k++] = a[i++];
        while (j <= r)
            tmp[k++] = a[j++];
        copy(tmp, tmp + k, a.begin() + l);
    }
    void merge_sort(vector<long> &a, int l, int r)
    {
        if (l >= r)
            return;
        int mid = l + r - l / 2;
        merge_sort(a, l, mid);
        merge_sort(a, mid + 1, r);

        int k = mid + 1, j = k;
        for (int i = l; i <= mid; ++i)
        {
            while (k <= r && a[k] - a[i] < lower)
                ++k;
            while (j <= r && a[j] - a[i] <= upper)
                ++j;
            res += j - k;
        }
        if (a[mid] <= a[mid + 1])
            return;
        merge(a, l, mid, r);
    }
    int countRangeSum(vector<int> &nums, int lower, int upper)
    {
        this->lower = lower;
        this->upper = upper;
        int n = nums.size();
        vector<long> presum(n + 1, 0);
        for (int i = 0; i < n; ++i)
            presum[i] = presum[i] + nums[i];
        merge_sort(presum, 0, n);
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
120

每日一练社区's avatar
每日一练社区 已提交
121
### A
F
fix bug  
feilong 已提交
122

每日一练社区's avatar
每日一练社区 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
```cpp
class Solution
{
public:
    int countRangeSum(vector<int> &nums, int lower, int upper)
    {
        int n = nums.size();
        long presum = 0;

        multiset<long> S;
        S.insert(0);
        int ret = 0;

        for (int i = 0; i < n; i++)
        {
            presum += nums[i];

            ret += distance(S.lower_bound(presum - upper), S.upper_bound(presum - lower));
            S.insert(presum);
        }
        return ret;
    }
};
```

### B
F
fix bug  
feilong 已提交
149

每日一练社区's avatar
每日一练社区 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
```cpp
class Solution
{
public:
    int countRangeSum(vector<int> &nums, int lower, int upper)
    {
        int n = nums.size();
        long presum = 0;
        vector<long> S(n + 1, 0);
        int ret = 0;
        for (int i = 1; i <= n; i++)
        {
            presum += nums[i - 1];
            for (int j = 1; j <= i; j++)
            {
                if (lower <= presum - S[j - 1] && presum - S[j - 1] <= upper)
                    ret++;
            }
            S[i] = presum;
        }
        return ret;
    }
};
```

### C
F
fix bug  
feilong 已提交
176

每日一练社区's avatar
每日一练社区 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
```cpp
class Solution
{
public:
    int countRangeSum(vector<int> &nums, int lower, int upper)
    {
        int count = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] <= upper && nums[i] >= lower)
                ++count;
            long sum = nums[i];
            for (int j = i + 1; j < nums.size(); ++j)
            {
                sum += nums[j];
                if (sum <= upper && sum >= lower)
                    ++count;
            }
        }
        return count;
    }
};
```