-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* @lc app=leetcode.cn id=303 lang=golang | ||
* | ||
* [303] 区域和检索 - 数组不可变 | ||
* | ||
* https://leetcode-cn.com/problems/range-sum-query-immutable/description/ | ||
* | ||
* algorithms | ||
* Easy (74.50%) | ||
* Likes: 451 | ||
* Dislikes: 0 | ||
* Total Accepted: 162.3K | ||
* Total Submissions: 217.4K | ||
* Testcase Example: '["NumArray","sumRange","sumRange","sumRange"]\n' + | ||
'[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]' | ||
* | ||
* 给定一个整数数组 nums,处理以下类型的多个查询: | ||
* | ||
* | ||
* 计算索引 left 和 right (包含 left 和 right)之间的 nums 元素的 和 ,其中 left <= right | ||
* | ||
* | ||
* 实现 NumArray 类: | ||
* | ||
* | ||
* NumArray(int[] nums) 使用数组 nums 初始化对象 | ||
* int sumRange(int i, int j) 返回数组 nums 中索引 left 和 right 之间的元素的 总和 ,包含 left 和 | ||
* right 两点(也就是 nums[left] + nums[left + 1] + ... + nums[right] ) | ||
* | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* | ||
* 输入: | ||
* ["NumArray", "sumRange", "sumRange", "sumRange"] | ||
* [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] | ||
* 输出: | ||
* [null, 1, -1, -3] | ||
* | ||
* 解释: | ||
* NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); | ||
* numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3) | ||
* numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1)) | ||
* numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1)) | ||
* | ||
* | ||
* | ||
* | ||
* 提示: | ||
* | ||
* | ||
* 1 <= nums.length <= 10^4 | ||
* -10^5 <= nums[i] <= 10^5 | ||
* 0 <= i <= j < nums.length | ||
* 最多调用 10^4 次 sumRange 方法 | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
type NumArray struct { | ||
PreSum []int | ||
} | ||
|
||
func Constructor(nums []int) NumArray { | ||
preSum := make([]int, len(nums)+1) | ||
|
||
// preSum[0] = 0 | ||
// preSum[1] = 0 + nums[0] | ||
for i := 1; i <= len(nums); i++ { | ||
preSum[i] = preSum[i-1] + nums[i-1] | ||
} | ||
|
||
return NumArray{ | ||
PreSum: preSum, | ||
} | ||
} | ||
|
||
func (this *NumArray) SumRange(left int, right int) int { | ||
return this.PreSum[right+1] - this.PreSum[left] | ||
|
||
} | ||
|
||
/** | ||
* Your NumArray object will be instantiated and called as such: | ||
* obj := Constructor(nums); | ||
* param_1 := obj.SumRange(left,right); | ||
*/ | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* @lc app=leetcode.cn id=560 lang=golang | ||
* | ||
* [560] 和为 K 的子数组 | ||
* | ||
* https://leetcode-cn.com/problems/subarray-sum-equals-k/description/ | ||
* | ||
* algorithms | ||
* Medium (45.01%) | ||
* Likes: 1491 | ||
* Dislikes: 0 | ||
* Total Accepted: 226.9K | ||
* Total Submissions: 502.8K | ||
* 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 code=start | ||
func subarraySum(nums []int, k int) int { | ||
pre, count := 0, 0 | ||
m := make(map[int]int, 0) | ||
m[0] = 1 | ||
for i := range nums { | ||
pre += nums[i] | ||
if v, ok := m[pre-k];ok{ | ||
count += v | ||
} | ||
m[pre] += 1 | ||
} | ||
return count | ||
} | ||
// @lc code=end | ||
|