Documentation ¶
Overview ¶
* @lc app=leetcode.cn id=304 lang=golang * * [304] 二维区域和检索 - 矩阵不可变 * * https://leetcode.cn/problems/range-sum-query-2d-immutable/description/ *
- algorithms
- Medium (59.62%)
- Likes: 419
- Dislikes: 0
- Total Accepted: 99.7K
- Total Submissions: 167K
- Testcase Example: '["NumMatrix","sumRegion","sumRegion","sumRegion"]\n' + '[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]'
* * 给定一个二维矩阵 matrix,以下类型的多个请求: * * * 计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。 * * * 实现 NumMatrix 类: * * * NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化 * int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) * 、右下角 (row2, col2) 所描述的子矩阵的元素 总和 。 * * * * * 示例 1: * * * * * 输入: * ["NumMatrix","sumRegion","sumRegion","sumRegion"] * * [[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]] * 输出: * [null, 8, 11, 12] * * 解释: * NumMatrix numMatrix = new * NumMatrix([[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]); * numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和) * numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和) * numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和) * * * * * 提示: * * * m == matrix.length * n == matrix[i].length * 1 <= m, n <= 200 * -10^5 <= matrix[i][j] <= 10^5 * 0 <= row1 <= row2 < m * 0 <= col1 <= col2 < n * 最多调用 10^4 次 sumRegion 方法 * *
* @lc app=leetcode.cn id=304 lang=golang * * [304] 二维区域和检索 - 矩阵不可变 * * https://leetcode.cn/problems/range-sum-query-2d-immutable/description/ *
- algorithms
- Medium (59.62%)
- Likes: 419
- Dislikes: 0
- Total Accepted: 99.7K
- Total Submissions: 167K
- Testcase Example: '["NumMatrix","sumRegion","sumRegion","sumRegion"]\n' + '[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]'
* * 给定一个二维矩阵 matrix,以下类型的多个请求: * * * 计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。 * * * 实现 NumMatrix 类: * * * NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化 * int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) * 、右下角 (row2, col2) 所描述的子矩阵的元素 总和 。 * * * * * 示例 1: * * * * * 输入: * ["NumMatrix","sumRegion","sumRegion","sumRegion"] * * [[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]] * 输出: * [null, 8, 11, 12] * * 解释: * NumMatrix numMatrix = new * NumMatrix([[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]); * numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和) * numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和) * numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和) * * * * * 提示: * * * m == matrix.length * n == matrix[i].length * 1 <= m, n <= 200 * -10^5 <= matrix[i][j] <= 10^5 * 0 <= row1 <= row2 < m * 0 <= col1 <= col2 < n * 最多调用 10^4 次 sumRegion 方法 * *
@lc app=leetcode.cn id=525 lang=golang *
[525] 连续数组 *
https://leetcode.cn/problems/contiguous-array/description/ *
algorithms
Medium (54.47%)
Likes: 573
Dislikes: 0
Total Accepted: 58.7K
Total Submissions: 107.7K
Testcase Example: '[0,1]' *
给定一个二进制数组 nums , 找到含有相同数量的 0 和 1 的最长连续子数组,并返回该子数组的长度。 * * *
示例 1: * *
输入: nums = [0,1]
输出: 2
说明: [0, 1] 是具有相同数量 0 和 1 的最长连续子数组。 *
示例 2: * *
输入: nums = [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。 * * *
提示: * *
1
nums[i] 不是 0 就是 1 * *
@lc app=leetcode.cn id=560 lang=golang *
[560] 和为 K 的子数组 *
https://leetcode.cn/problems/subarray-sum-equals-k/description/ *
algorithms
Medium (45.38%)
Likes: 1599
Dislikes: 0
Total Accepted: 250.7K
Total Submissions: 552.4K
Testcase Example: '[1,1,1]\n2' *
给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的连续子数组的个数 。 * * *
示例 1: * *
输入:nums = [1,1,1], k = 2
输出:2 * *
示例 2: * *
输入:nums = [1,2,3], k = 3
输出:2 * * * *
提示: * *
1 <= nums.length <= 2 * 10^4
-1000 <= nums[i] <= 1000
-10^7 <= k <= 10^7 * *
@lc app=leetcode.cn id=724 lang=golang *
[724] 寻找数组的中心下标 *
https://leetcode.cn/problems/find-pivot-index/description/ *
algorithms
Easy (49.73%)
Likes: 445
Dislikes: 0
Total Accepted: 209.1K
Total Submissions: 420K
Testcase Example: '[1,7,3,6,5,6]' *
给你一个整数数组 nums ,请计算数组的 中心下标 。 *
数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。 *
如果中心下标位于数组最左端,那么左侧数之和视为 0 ,因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。 *
如果数组有多个中心下标,应该返回 最靠近左边 的那一个。如果数组不存在中心下标,返回 -1 。 * * *
示例 1: * *
输入:nums = [1, 7, 3, 6, 5, 6]
输出:3
解释:
中心下标是 3 。
左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,
右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ,二者相等。 * *
示例 2: * *
输入:nums = [1, 2, 3]
输出:-1
解释:
数组中不存在满足此条件的中心下标。 *
示例 3: * *
输入:nums = [2, 1, -1]
输出:0
解释:
中心下标是 0 。
左侧数之和 sum = 0 ,(下标 0 左侧不存在元素),
右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。 * * *
提示: * *
1 <= nums.length <= 10^4
-1000 <= nums[i] <= 1000 * * * *
注意:本题与主站 1991
题相同:https://leetcode-cn.com/problems/find-the-middle-index-in-array/ *
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NumMatrix ¶
type NumMatrix struct {
// contains filtered or unexported fields
}
@lc code=start
func Constructor ¶
{3, 0, 1, 4, 2}, {5, 6, 3, 2, 1}, {1, 2, 0, 1, 5}, {4, 1, 0, 1, 7}, {1, 0, 3, 0, 5},