{"id":3127,"date":"2018-07-14T07:59:05","date_gmt":"2018-07-14T14:59:05","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3127"},"modified":"2018-07-14T09:43:11","modified_gmt":"2018-07-14T16:43:11","slug":"leetcode-523-continuous-subarray-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-523-continuous-subarray-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 523. Continuous Subarray Sum"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a list of\u00a0<b>non-negative<\/b>\u00a0numbers and a target\u00a0<b>integer<\/b>\u00a0k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of\u00a0<b>k<\/b>, that is, sums up to n*k where n is also an\u00a0<b>integer<\/b>.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [23, 2, 4, 6, 7],  k=6\r\n<b>Output:<\/b> True\r\n<b>Explanation:<\/b> Because [2, 4] is a continuous subarray of size 2 and sums up to 6.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [23, 2, 6, 4, 7],  k=6\r\n<b>Output:<\/b> True\r\n<b>Explanation:<\/b> Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the array won&#8217;t exceed 10,000.<\/li>\n<li>You may assume the sum of all the numbers is in the range of a signed 32-bit integer.<\/li>\n<\/ol>\n<h1><strong>Special case: <\/strong><\/h1>\n<p>nums = [0,0], k = 0, return = True<\/p>\n<h1><strong>Solution: Prefix Sum Reminder<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(min(n, k))<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms (&lt;99.62%)\r\nclass Solution {\r\npublic:\r\n  bool checkSubarraySum(vector&lt;int&gt;&amp; nums, int k) {    \r\n    unordered_map&lt;int, int&gt; m; \/\/ sum % k -&gt; first index\r\n    m[0] = -1;\r\n    int sum = 0;\r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      sum += nums[i];\r\n      if (k != 0) sum %= k;      \r\n      if (m.count(sum)) {\r\n        if (i - m.at(sum) &gt; 1) return true;\r\n      } else {\r\n        m[sum] = i;\r\n      }\r\n    }\r\n    return false;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-560-subarray-sum-equals-k\/\">\u82b1\u82b1\u9171 LeetCode 560. Subarray Sum Equals K<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-525-contiguous-array\/\">\u82b1\u82b1\u9171 LeetCode 525. Contiguous Array<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a list of\u00a0non-negative\u00a0numbers and a target\u00a0integer\u00a0k, write a function to check if the array has a continuous subarray of size at least 2&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[327,177,200,156,41,62],"class_list":["post-3127","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-k","tag-medium","tag-prefix-sum","tag-reminder","tag-subarray","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3127"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3127\/revisions"}],"predecessor-version":[{"id":3136,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3127\/revisions\/3136"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}