3 questions to do it this week.
Question 1:
Contest page address: https: //leetcode-cn.com/problems/decompress-run-length-encoded-list/
Java code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public int[] decompressRLElist(int[] nums) {
int len = nums.length;
List<Integer> res = new ArrayList<>(len);
for (int i = 0; i < len / 2; i++) {
int count = nums[2 * i];
int val = nums[2 * i + 1];
for (int j = 0; j < count; j++) {
res.add(val);
}
}
return res.stream().mapToInt(Integer::intValue).toArray();
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
Solution solution = new Solution();
int[] res = solution.decompressRLElist(nums);
System.out.println(Arrays.toString(res));
}
}
Problem 2:
Contest page address: https: //leetcode-cn.com/problems/matrix-block-sum/
Refer to "stay button" on page 304 title: [ "two-dimensional area and retrieval - Matrix immutable"] (two-dimensional area and retrieval - Matrix immutable) approach.
Ideas:
- First computing a matrix and prefix;
- Then calculate coordinates of the upper left corner and the lower right corner of the effective region of;
- And then use the matrix to the prefix computational complexity and area.
Java code:
import java.util.Arrays;
public class Solution {
/**
* 前缀和矩阵
*/
private int[][] preSum;
public int[][] matrixBlockSum(int[][] mat, int K) {
// 行数和列数不用特判,因为题目已经说了不为 0
int rows = mat.length;
int cols = mat[0].length;
// 初始化的时候多设置一行,多设置一列
preSum = new int[rows + 1][cols + 1];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
preSum[i + 1][j + 1] = preSum[i + 1][j] + preSum[i][j + 1] - preSum[i][j] + mat[i][j];
}
}
int[][] res = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 左上角横纵坐标
int row1 = Math.max(i - K, 0);
int col1 = Math.max(j - K, 0);
// 右下角横纵坐标
int row2 = Math.min(i + K, rows - 1);
int col2 = Math.min(j + K, cols - 1);
res[i][j] = sumRegion(row1, col1, row2, col2);
}
}
return res;
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return preSum[row2 + 1][col2 + 1]
- preSum[row1][col2 + 1]
- preSum[row2 + 1][col1]
+ preSum[row1][col1];
}
}
Complexity analysis :
- time complexity: , where is the number of rows of the matrix, is the number of columns of the matrix;
- Space complexity: .
Question 3:
Contest page address: https: //leetcode-cn.com/contest/biweekly-contest-17/problems/sum-of-nodes-with-even-valued-grandparent/
Thinking: traversal sequence.
- If the current value is an even number of nodes, child nodes to it a tag (the value of the modified child node negative).
- When dequeuing value detecting node. If the value of the node is negative, it's child node on the way into the team when the value of all add up.
- Because the values before modification has the added result, after the completion of traversing nodes do not need to modify the value back.
Java code:
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public int sumEvenGrandparent(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int res = 0;
while (!queue.isEmpty()) {
TreeNode top = queue.poll();
boolean flag = false;
if ((top.val & 1) == 0) {
// 表示它的孩子节点开始收集工作
flag = true;
}
if (top.left != null) {
if (top.val < 0) {
res += top.left.val;
}
if (flag) {
top.left.val *= -1;
}
queue.add(top.left);
}
if (top.right != null) {
if (top.val < 0) {
res += top.right.val;
}
if (flag) {
top.right.val *= -1;
}
queue.add(top.right);
}
}
return res;
}
}