当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 10: And for k Subarray
subject 1:
Ideas 1: violence
Time complexity O(n2)
Code
func subarraySum(nums []int, k int) int {
//start: 13:51, 13:57
// Ideas 1: violence , Because there may be negative numbers in elements
// Fix a number i, Record the possibility behind
// Parameter checking
if len(nums) == 0 {
return 0
}
// violence
count := 0
for i := 0; i < len(nums); i++{
sum := 0
for j := i; j < len(nums); j++{
sum += nums[j]
if sum == k {
count ++
}
}
}
return count
}
test 1

Ideas 2: The prefix and + hashmap
Because there are negative numbers , Therefore, sliding windows cannot be used
Code
func subarraySum(nums []int, k int) int {
//start: 13:51, 13:57
// Ideas 2: The prefix and + hashmap
//
// Parameter checking
if len(nums) == 0 {
return 0
}
// Prefix and + hashmap
sumToCount := make(map[int]int)
sum,resCount := 0,0
sumToCount[0] = 1
for i := 0; i < len(nums); i ++{
sum += nums[i]
// There is sum - sumOld = k, Add to result
resCount += sumToCount[sum - k]
sumToCount[sum] += 1
}
return resCount
}
test

边栏推荐
- [offer9] implement queues with two stacks
- dosbox第一次使用
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- JUC forkjoin and completable future
- Programming homework: educational administration management system (C language)
- 微信小程序开发心得
- Acwing-116 pilot brother
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
- 平衡二叉树详解 通俗易懂
猜你喜欢
随机推荐
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
[算法] 剑指offer2 golang 面试题1:整数除法
Matlab读取GNSS 观测值o文件代码示例
[算法] 剑指offer2 golang 面试题2:二进制加法
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Latex learning
[leetcode15] sum of three numbers
Fairygui loop list
[offer9]用两个栈实现队列
Introduction to the daily practice column of the Blue Bridge Cup
[899]有序队列
[算法] 劍指offer2 golang 面試題2:二進制加法
[leetcode622] design circular queue
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Unity scene jump and exit
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Unity3d, Alibaba cloud server, platform configuration
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Fabrication d'un sac à dos simple fairygui
341. Flatten nested list iterator
![[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等](/img/11/ee0628a68542236fc641966579a31a.png)







![[算法] 剑指offer2 golang 面试题1:整数除法](/img/e6/f17135207b3540ec58e5a9eed54220.png)
