Skip to content

Commit 5bc12ed

Browse files
committed
2
1 parent e4783c4 commit 5bc12ed

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
217217
#### [299. Bulls and Cows](https://github.com/hitzzc/go-leetcode/tree/master/bulls_and_cows)
218218
#### [300. Longest Increasing Subsequence](https://github.com/hitzzc/go-leetcode/tree/master/longest_increasing_subsequence)
219219
#### [301. Remove Invalid Parentheses](https://github.com/hitzzc/go-leetcode/tree/master/remove_invalid_parentheses)
220+
#### [303. Range Sum Query - Immutable](https://github.com/hitzzc/go-leetcode/tree/master/range_sum_query_immutable)
221+
#### [304. Range Sum Query 2D - Immutable](https://github.com/hitzzc/go-leetcode/tree/master/range_sum_query_immutable)
222+
220223

221224

222225

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class NumMatrix {
5+
vector<vector<int>> records;
6+
public:
7+
NumMatrix(vector<vector<int>> &matrix) {
8+
int row = matrix.size();
9+
int col = row == 0 ? 0 : matrix[0].size();
10+
records = vector<vector<int>>(row+1, vector<int>(col+1, 0));
11+
for (int i = 0; i < row; ++i) {
12+
for (int j = 0; j < col; ++j) {
13+
records[i+1][j+1] = records[i][j+1] + records[i+1][j] - records[i][j] + matrix[i][j];
14+
}
15+
}
16+
}
17+
18+
int sumRegion(int row1, int col1, int row2, int col2) {
19+
return records[row2+1][col2+1] - records[row2+1][col1] - records[row1][col2+1] + records[row1][col1];
20+
}
21+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class NumArray {
5+
vector<int> fn;
6+
public:
7+
NumArray(vector<int> &nums): fn(vector<int>(nums.size()+1)) {
8+
fn[0] = 0;
9+
for (int i = 0; i < nums.size(); ++i) {
10+
fn[i+1] = fn[i] + nums[i];
11+
}
12+
}
13+
14+
int sumRange(int i, int j) {
15+
return fn[j] - fn[i-1];
16+
}
17+
};

0 commit comments

Comments
 (0)