Skip to content

Commit 20e370d

Browse files
committed
update
1 parent 49da38a commit 20e370d

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @lc app=leetcode.cn id=303 lang=golang
3+
*
4+
* [303] 区域和检索 - 数组不可变
5+
*
6+
* https://leetcode-cn.com/problems/range-sum-query-immutable/description/
7+
*
8+
* algorithms
9+
* Easy (74.50%)
10+
* Likes: 451
11+
* Dislikes: 0
12+
* Total Accepted: 162.3K
13+
* Total Submissions: 217.4K
14+
* Testcase Example: '["NumArray","sumRange","sumRange","sumRange"]\n' +
15+
'[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]'
16+
*
17+
* 给定一个整数数组  nums,处理以下类型的多个查询:
18+
*
19+
*
20+
* 计算索引 left 和 right (包含 left 和 right)之间的 nums 元素的 和 ,其中 left <= right
21+
*
22+
*
23+
* 实现 NumArray 类:
24+
*
25+
*
26+
* NumArray(int[] nums) 使用数组 nums 初始化对象
27+
* int sumRange(int i, int j) 返回数组 nums 中索引 left 和 right 之间的元素的 总和 ,包含 left 和
28+
* right 两点(也就是 nums[left] + nums[left + 1] + ... + nums[right] )
29+
*
30+
*
31+
*
32+
*
33+
* 示例 1:
34+
*
35+
*
36+
* 输入:
37+
* ["NumArray", "sumRange", "sumRange", "sumRange"]
38+
* [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
39+
* 输出:
40+
* [null, 1, -1, -3]
41+
*
42+
* 解释:
43+
* NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
44+
* numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
45+
* numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
46+
* numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
47+
*
48+
*
49+
*
50+
*
51+
* 提示:
52+
*
53+
*
54+
* 1 <= nums.length <= 10^4
55+
* -10^5 <= nums[i] <= 10^5
56+
* 0 <= i <= j < nums.length
57+
* 最多调用 10^4 次 sumRange 方法
58+
*
59+
*
60+
*/
61+
62+
// @lc code=start
63+
type NumArray struct {
64+
PreSum []int
65+
}
66+
67+
func Constructor(nums []int) NumArray {
68+
preSum := make([]int, len(nums)+1)
69+
70+
// preSum[0] = 0
71+
// preSum[1] = 0 + nums[0]
72+
for i := 1; i <= len(nums); i++ {
73+
preSum[i] = preSum[i-1] + nums[i-1]
74+
}
75+
76+
return NumArray{
77+
PreSum: preSum,
78+
}
79+
}
80+
81+
func (this *NumArray) SumRange(left int, right int) int {
82+
return this.PreSum[right+1] - this.PreSum[left]
83+
84+
}
85+
86+
/**
87+
* Your NumArray object will be instantiated and called as such:
88+
* obj := Constructor(nums);
89+
* param_1 := obj.SumRange(left,right);
90+
*/
91+
// @lc code=end
92+

560.和为-k-的子数组.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @lc app=leetcode.cn id=560 lang=golang
3+
*
4+
* [560] 和为 K 的子数组
5+
*
6+
* https://leetcode-cn.com/problems/subarray-sum-equals-k/description/
7+
*
8+
* algorithms
9+
* Medium (45.01%)
10+
* Likes: 1491
11+
* Dislikes: 0
12+
* Total Accepted: 226.9K
13+
* Total Submissions: 502.8K
14+
* Testcase Example: '[1,1,1]\n2'
15+
*
16+
* 给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的子数组的个数 。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:nums = [1,1,1], k = 2
24+
* 输出:2
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:nums = [1,2,3], k = 3
31+
* 输出:2
32+
*
33+
*
34+
*
35+
*
36+
* 提示:
37+
*
38+
*
39+
* 1 <= nums.length <= 2 * 10^4
40+
* -1000 <= nums[i] <= 1000
41+
* -10^7 <= k <= 10^7
42+
*
43+
*
44+
*/
45+
46+
// @lc code=start
47+
func subarraySum(nums []int, k int) int {
48+
pre, count := 0, 0
49+
m := make(map[int]int, 0)
50+
m[0] = 1
51+
for i := range nums {
52+
pre += nums[i]
53+
if v, ok := m[pre-k];ok{
54+
count += v
55+
}
56+
m[pre] += 1
57+
}
58+
return count
59+
}
60+
// @lc code=end
61+

0 commit comments

Comments
 (0)