LeetCode #974. Subarray Sums Divisible by K 数组
2021-01-15 16:11
标签:ber tput https sub case turn hand subarray 空间复杂度 Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Note: 暴力解法,不多说明了。 时间复杂度:O(n^3) 前缀和取模,然后进行排列组合,坑点是负数取模的处理,以及取模后为 0 时的处理。 时间复杂度:O(n) 耗时 96 ms, faster than 54 %, Memory 30.3 MB LeetCode #974. Subarray Sums Divisible by K 数组 标签:ber tput https sub case turn hand subarray 空间复杂度 原文地址:https://www.cnblogs.com/Bw98blogs/p/12936381.htmlDescription
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
思路
解法一
空间复杂度:O(1)解法二
空间复杂度:O(n)class Solution {
public:
int subarraysDivByK(const vector
参考
文章标题:LeetCode #974. Subarray Sums Divisible by K 数组
文章链接:http://soscw.com/index.php/essay/42301.html