1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
subarray-sums-divisible-by-k.cpp 366 Bytes
Copy Edit Raw Blame History
// Time: O(n)
// Space: O(k)
class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
unordered_map<int, int> count;
count[0] = 1;
int prefix = 0, result = 0;
for (const auto& a : A) {
prefix = (prefix + (a % K + K)) % K;
result += count[prefix]++;
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

Search