523. Continuous Subarray Sum
程序员文章站
2022-04-25 09:51:45
...
顺序扫描数组,并存储每一个位置的sum与k的余数,当余数重复出现时,即产生解;
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
unordered_map<int,int> table;
int sum=0;
int flag;
table[0] = -1;
for(int i=0;i<nums.size();i++)
{
sum += nums[i];
if(k==0)
flag=sum;
else
flag= (sum%k);
if(table.find(flag)==table.end())
table[flag]= i;
else
{
if(i-table[flag] > 1)
return true;
}
}
return false;
}
};
上一篇: IBM系列显示器检修实例
推荐阅读
-
HK Maximum Subarray Sum
-
LintCode 139. Subarray Sum Closest
-
Minimum Size Subarray Sum
-
Minimum Size Subarray Sum
-
LeetCode 560 Subarray Sum Equals K (hash)
-
Leetcode0523--Continuous Subarray Sum 连续和倍数
-
LeetCode-209-Minimum Size Subarray Sum
-
LeetCode题解——862.Shortest Subarray with Sum at Least K
-
(M)Dynamic Programming:523. Continuous Subarray Sum
-
leetcode 523. Continuous Subarray Sum