Skip to content

Commit a923592

Browse files
committed
[not completed]leetcode contest biweekly 17
1 parent 5334d23 commit a923592

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* AC:
3+
* 思路:较易,略
4+
*/
5+
class Solution {
6+
public:
7+
vector<int> decompressRLElist(vector<int>& nums) {
8+
vector<int> ret;
9+
for(int i = 0; i < nums.size() / 2; i++)
10+
for(int j = 0; j < nums[2 * i]; j++)
11+
ret.push_back(nums[2 * i + 1]);
12+
13+
return ret;
14+
}
15+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* AC:
3+
* 思路:先按行预先计算出行的累加和,用一个 a[row + 2 * K][col + 2 * K] 的数组记录,
4+
* 然后在对 a 遍历,每个 [i][j] 位置累加 a[i][j] 到 a[i + 2 * K][j + 2 * K] 的K^2 个元素和
5+
* 这样总体复杂度是 O(m)(n)
6+
* T: O(m * n * K), 由于 K 是常数,所以接近 m * n 的复杂度
7+
*/
8+
class Solution {
9+
public:
10+
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int K) {
11+
int row = mat.size(), col = mat[0].size();
12+
vector<vector<int>> ret(row, vector<int>(col, 0));
13+
vector<vector<int>> record(row + 2 * K, vector<int>(row + 2 * K, 0));
14+
for(int i = 0; i < row; i++) {
15+
for(int j = 0; j < col; j++) {
16+
for(int k = -K; k <= K; k++) {
17+
if(j + k < 0 || j + k > col - 1) // 左右越界的部分不计算行的累加和
18+
continue;
19+
record[i + K][j + K] += mat[i][j + k];
20+
}
21+
}
22+
}
23+
24+
for(int i = 0; i < row; i++) {
25+
for(int j = 0; j < col; j++) {
26+
int temp = 0;
27+
for(int k = -K; k <= K; k++) {
28+
temp += record[i + K - k][j + K];
29+
}
30+
ret[i][j] = temp;
31+
}
32+
}
33+
34+
return ret;
35+
}
36+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int sumEvenGrandparent(TreeNode* root) {
13+
14+
}
15+
};

0 commit comments

Comments
 (0)