go: add «523. Continuous Subarray Sum»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
bcb4538b17
commit
adc9be2dbc
1 changed files with 22 additions and 0 deletions
22
go/continuous-subarray-sum.go
Normal file
22
go/continuous-subarray-sum.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package continuous_subarray_sum
|
||||
|
||||
func checkSubarraySum(nums []int, k int) bool {
|
||||
first_index_of := make(map[int]int)
|
||||
first_index_of[0] = -1
|
||||
|
||||
remainder := 0
|
||||
for i, x := range nums {
|
||||
remainder = (remainder + x) % k
|
||||
|
||||
j, seen := first_index_of[remainder]
|
||||
if seen {
|
||||
if i-j > 1 {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
first_index_of[remainder] = i
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
Loading…
Reference in a new issue