7.1 303. 区域和检索 - 数组不可变

  1. class NumArray {
  2. public:
  3. vector<int> preSum;
  4. //构造前缀和
  5. NumArray(vector<int>& nums) {
  6. int n=nums.size();
  7. preSum.resize(n+1);
  8. for(int i=0;i<n;i++){
  9. preSum[i+1]=preSum[i]+nums[i];
  10. }
  11. }
  12. int sumRange(int left, int right) {
  13. return preSum[right+1]-preSum[left];
  14. }
  15. };

7.2 304. 二维区域和检索 - 矩阵不可变

  1. class NumMatrix {
  2. public:
  3. vector<vector<int>> preSum;
  4. NumMatrix(vector<vector<int>>& matrix) {
  5. int m=matrix.size();
  6. if(m>0){
  7. int n=matrix[0].size();
  8. preSum.resize(m+1,vector<int>(n+1));
  9. for(int i=0;i<m;i++){
  10. for(int j=0;j<n;j++){
  11. //有重叠,注意要减去
  12. preSum[i+1][j+1]=preSum[i][j+1]+preSum[i+1][j]+matrix[i][j]-preSum[i][j];
  13. }
  14. }
  15. }
  16. }
  17. int sumRegion(int row1, int col1, int row2, int col2) {
  18. //有重叠,注意要加回来
  19. return preSum[row2+1][col2+1]-preSum[row1][col2+1]-preSum[row2+1][col1]+preSum[row1][col1];
  20. }
  21. };

7.3 560. 和为 K 的子数组

  1. class Solution {
  2. public:
  3. int subarraySum(vector<int>& nums, int k) {
  4. //定义哈希表存放前缀和
  5. unordered_map<int,int> preSum;
  6. preSum[0]=1;
  7. //转换为 preSum[j]==preSum[i]-k;
  8. int count=0,sum_i=0;
  9. for(int i=0;i<nums.size();i++){
  10. sum_i+=nums[i];
  11. //nums[0...j]
  12. int sum_j=sum_i-k;
  13. //如果前边有这个前缀和,直接更新
  14. if(preSum[sum_j]!=0){
  15. count+=preSum[sum_j];
  16. }
  17. //把nums[0...i]加入preSum中
  18. preSum[sum_i]++;
  19. }
  20. return count;
  21. }
  22. };

7.4 区间加法

7. 前缀和 - 图1

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. //差分数组
  5. class Solution {
  6. public:
  7. vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
  8. vector<int> diff(length,0);
  9. for(auto &update:updates){
  10. int start=update[0],end=update[1],val=update[2];
  11. diff[start]+=val;
  12. if(end<length-1){
  13. diff[end+1]-=val;
  14. }
  15. }
  16. for(int i=1;i<length;i++){
  17. diff[i]+=diff[i-1];
  18. }
  19. return diff;
  20. }
  21. };
  22. int main(){
  23. int length=5;
  24. int n=3;
  25. vector<vector<int>> updates;
  26. vector<int> result(length,0);
  27. vector<int> v1;
  28. vector<int> v2;
  29. vector<int> v3;
  30. v1.push_back(1);
  31. v1.push_back(3);
  32. v1.push_back(2);
  33. v2.push_back(2);
  34. v2.push_back(4);
  35. v2.push_back(3);
  36. v3.push_back(0);
  37. v3.push_back(2);
  38. v3.push_back(-2);
  39. updates.push_back(v1);
  40. updates.push_back(v2);
  41. updates.push_back(v3);
  42. Solution s;
  43. for(int i=0;i<n;i++){
  44. cout<<"[ ";
  45. for(int j=0;j<n;j++){
  46. cout<<updates[i][j]<<" ";
  47. }
  48. cout<<"] ";
  49. }
  50. cout<<endl;
  51. result=s.getModifiedArray(length,updates);
  52. for(int i=0;i<length;i++){
  53. cout<<result[i]<<" ";
  54. }
  55. cout<<endl;
  56. system("pause");
  57. return 0;
  58. }

7.5 1094. 拼车

class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        vector<int> res(1001,0),diff(1001,0);
        for(auto &trip:trips){
            int val=trip[0],from=trip[1],to=trip[2];
            diff[from]+=val;
            //并不包含to,这个时候已经不在车上了
            if(to<1000){                
                diff[to]-=val;
            }
        }
        res[0]=diff[0];
        if(res[0]>capacity){
            return false;
        }
        for(int i=1;i<1001;i++){
            res[i]=diff[i]+res[i-1];
            if(res[i]>capacity){
                return false;
            }
        }
        return true;
    }
};

7.6 1109. 航班预订统计

class Solution {
public:
    vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
        vector<int> diff(n,0);
        //取出每个预定记录进行计算
        for(auto& booking:bookings){
            int start=booking[0]-1;
            int end=booking[1]-1;
            int val=booking[2];
            diff[start]+=val;
            if(end<n-1){
                diff[end+1]-=val;
            }
        }
        //还原数组
        for(int i=1;i<n;i++){
            diff[i]+=diff[i-1];
        }
        return diff;
    }
};