首页 > 其他 > 详细

523

时间:2017-05-07 17:26:24      阅读:485      评论:0      收藏:0      [点我收藏+]

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

 

 1 class Solution {
 2 public:
 3     bool checkSubarraySum(vector<int>& nums, int k) {
 4         int n=nums.size(),pre=0,sum=0;
 5         unordered_set<int>mod_k;
 6         for(int i=0;i<n;i++)
 7         {
 8             sum+=nums[i];
 9             int mod=k==0?sum:sum%k;
10             if(mod_k.count(mod))
11                 return true;
12             mod_k.insert(pre);
13             pre=mod;
14         }
15         return false;
16     }
17 };

 

523

原文:http://www.cnblogs.com/KRCheung/p/6821328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!