给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:
子数组大小 至少为 2 ,且
子数组元素总和为 k 的倍数。
如果存在,返回 true ;否则,返回 false 。
如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。0 始终视为 k 的一个倍数。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/continuous-subarray-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.HashMap; import java.util.Map; class Solution { public boolean checkSubarraySum(int[] nums, int k) { if (nums == null || nums.length < 2) { return false; } Map<Integer, Integer> helper = new HashMap<>(); helper.put(0, -1); int sum = 0; for (int i = 0; i < nums.length; ++i) { sum = (sum + nums[i]) % k; Integer index = helper.get(sum); if (index != null && i - index >= 2) { return true; } helper.putIfAbsent(sum, i); } return false; } }