523. Continuous Subarray Sum ⭐
Medium
Given an integer array nums
and an integer k
, return true
if nums
has a continuous subarray of size at least two whose elements sum up to a multiple of k
, or false
otherwise.
An integer x
is a multiple of k
if there exists an integer n
such that x = n * k
. 0
is always a multiple of k
.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 109
0 <= sum(nums[i]) <= 231 - 1
1 <= k <= 231 - 1
解題
這一題使用到前綴和的概念與mod的概念。首先,我們建立一個map來記錄目前 sum % k 的餘數,如果該數出現第二遍,代表 上一次出現該餘數的 index+1 到目前index 的值加總等於 k ,所以餘數才會再次出現。
Last updated