[email protected] [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable

https://leetcode.com/problems/range-sum-query-immutable/

class NumArray {

public: vector<int> vec;
public:
    NumArray(vector<int> &nums) {
        if(nums.empty()) return;
        else {
            vec.push_back(nums[0]);
            for(int i=1;i<nums.size();++i) vec.push_back(vec[i-1] + nums[i]);
        }
    }

    int sumRange(int i, int j) {
        if(i<0 || j>=vec.size()) return 0;
        if(i==0) return vec[j];
        else return vec[j] - vec[i-1];
    }
};

// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

https://leetcode.com/problems/range-sum-query-2d-immutable/

class NumMatrix {
public:
    vector<vector<int>> vec;
    NumMatrix(vector<vector<int>> &matrix) {
        if(matrix.size() == 0 || matrix[0].size() == 0) return;
        vec.resize(matrix.size());
        for(int i=0;i<vec.size();++i) vec[i].resize(matrix[0].size());

        vec[0][0] = matrix[0][0];
        for(int col=1;col<vec[0].size();++col) vec[0][col] = matrix[0][col] + vec[0][col-1];

        for(int row=1;row<vec.size();++row) {
            for(int col=0;col<vec[row].size();++col) {
                if(col == 0) vec[row][col] = matrix[row][col];
                else vec[row][col] = matrix[row][col] + vec[row][col-1];
            }
        }

        for(int row=1;row<vec.size();++row) {
            for(int col=0;col<vec[row].size();++col) {
                vec[row][col] += vec[row-1][col];
            }
        }

        for(int i=0;i<vec.size();++i) {
            for(int j=0;j<vec[i].size();++j) cout<<vec[i][j]<<" ";
            cout<<endl;
        }
    }
    int sumRegion(int row1, int col1, int row2, int col2) {
        if(row1<0 || row2>=vec.size() || col1<0 || col2>=vec[0].size()) return 0;

        int leftTop = 0;
        if(row1 == 0 || col1 == 0) leftTop = 0;
        else if(row1 && col1) leftTop = vec[row1-1][col1-1];

        int ret1 = 0;
        if(row1 == 0) ret1 = vec[row2][col2];
        else if(row1 > 0) ret1 = vec[row2][col2] - vec[row1-1][col2];

        int ret2 = 0;
        if(col1 == 0) ret2 = ret1;
        else if(col1 > 0) ret2 = ret1 - vec[row2][col1-1] + leftTop;

        return ret2;
    }
};

// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);

时间: 2024-09-29 21:03:07

[email protected] [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable的相关文章

304. Range Sum Query 2D - Immutable

/* * 304. Range Sum Query 2D - Immutable * 2016-7-3 by Mingyang * 这个题目自己做了一下同样的dp思路,但是out of boundry,为什么呢? * 因为自己老老实实的把dp的维度跟matrix的维度设成一样的了 * 那我们就把dp的维度再多设一个这样就很好地表示了 * dp[i][j]仅仅表示到i-1,j-1这个点(inclusive)的和的大小 */ class NumMatrix { private int[][] df;

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

[LeetCode] Range Sum Query - Immutable &amp; Range Sum Query 2D - Immutable

Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You

[email&#160;protected] [327] Count of Range Sum (Binary Search)

https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), incl

[Coding Made Simple] Sum Query in 2D Immutable Array

Given a 2D immutable array,  Write an efficient program to support any given sub-rectangle sum query in this 2D matrix. A simple solution is to add each entry inside the sub-rectangle. The runtime is O(n * m). The problem of this solution is that for

@bzoj - [email&#160;protected] [POI2015] Logistyka

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 维护一个长度为 n 的序列,一开始都是 0,支持以下两种操作: 1.U k a 将序列中第 k 个数修改为 a. 2.Z c s 在这个序列上,每次选出 c 个正数,并将它们都减去 1,询问能否进行 s 次操作. 每次询问独立,即每次询问不会对序列进行修改. input 第一行包含两个

@noi.ac - [email&#160;protected] game

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 小 Q 和小 T 正在玩一种双人游戏.m 张木牌从左往右排成一排,第 i 张木牌上写着一个正整数 bi.小 Q 和小 T 轮流行动总计 m 轮,小 Q 先手.在每一轮中,行动方需要选择最左或者最右的一张木牌并将其拿走.游戏最后每个人的得分即为他拿走的木牌上写着的数字之和,得分较大的一方

@atcoder - [email&#160;protected] Colorful Tree

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 题面明天起来补... @[email protected] 跟颜色有关,虽然题目中说是要"修改"实际上每一个询问之间是独立的,即修改不会有时效性. 所以不难想到可以使用树上莫队. 首先将边的颜色与边权下放至点.然后莫队时,统计出区间内每个点的点权(就是下放的边权)和 S.区间

@uoj - [email&#160;protected] 共价大爷游长沙

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 火车司机出秦川,跳蚤国王下江南,共价大爷游长沙.每个周末,勤劳的共价大爷都会开车游历长沙市. 长沙市的交通线路可以抽象成为一个 n 个点 n?1 条边的无向图,点编号为 1 到 n,任意两点间均存在恰好一条路径,显然两个点之间最多也只会有一条边相连.有一个包含一些点对 (x,y) 的可