{"id":352293,"date":"2021-02-13T17:37:18","date_gmt":"2021-02-13T09:37:18","guid":{"rendered":"http:\/\/4563.org\/?p=352293"},"modified":"2021-02-13T17:37:18","modified_gmt":"2021-02-13T09:37:18","slug":"%e4%b9%9d%e7%ab%a0%e7%ae%97%e6%b3%95-%e8%85%be%e8%ae%af%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%92%8c%e7%9b%b8%e5%90%8c%e7%9a%84%e4%ba%8c%e5%85%83%e5%ad%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=352293","title":{"rendered":"\u4e5d\u7ae0\u7b97\u6cd5 | \u817e\u8baf\u9762\u8bd5\u9898\uff1a\u548c\u76f8\u540c\u7684\u4e8c\u5143\u5b50\u6570\u7ec4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e5d\u7ae0\u7b97\u6cd5 | \u817e\u8baf\u9762\u8bd5\u9898\uff1a\u548c\u76f8\u540c\u7684\u4e8c\u5143\u5b50\u6570\u7ec4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><strong>\u63cf\u8ff0<\/strong><\/p>\n<p>\u5728\u7531\u82e5\u5e72 0 \u548c 1 \u7ec4\u6210\u7684\u6570\u7ec4 A \u4e2d\uff0c\u6709\u591a\u5c11\u4e2a\u548c\u4e3a S \u7684<strong>\u975e\u7a7a<\/strong>\u5b50\u6570\u7ec4\u3002<\/p>\n<ul>\n<li>A.length &lt;= 30000<\/li>\n<li>0 &lt;= S &lt;= A.length<\/li>\n<li>A[i] \u4e3a 0 \u6216 1<\/li>\n<\/ul>\n<p>[\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740](https:\/\/www.lintcode.com\/problem\/binary-subarrays-with-sum\/?utm_source=sc-zhihuzl-sy)<\/p>\n<p><strong>\u6837\u4f8b 1<\/strong><\/p>\n<pre><code>Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation:  The 4 subarrays are bolded below: [1,0,1] [1,0,1] [1,0,1,0] [0,1,0,1]  <\/code><\/pre>\n<p><strong>\u6837\u4f8b 2<\/strong><\/p>\n<pre><code>Input: A = [0,0,0,0,0,0,1,0,0,0], S = 0 Output: 27 Explanation:  And 27 subarrays for S.  <\/code><\/pre>\n<p><strong>\u9898\u89e3<\/strong><\/p>\n<p>\u524d\u7f00\u548c\uff1a\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4 sumN+1\uff0csi\u8868\u793a\u6570\u7ec4 A \u4e2d\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\uff0c\u7136\u540e\u904d\u5386 sum \u6570\u7ec4\uff0c\u8ba1\u7b97 si+S(\u542b\u4e49\uff1a\u524d i \u4e2a\u5143\u7d20\u4e4b\u548c\u662f si,\u627e\u548c\u4e3a S \u7684\u5b50\u6570\u7ec4\u4e2a\u6570)\u3002\u6c42 si+S \u7684\u4e2a\u6570<\/p>\n<pre><code>public class Solution {     \/**      * @param A: an array      * @param S: the sum      * @return: the number of non-empty subarrays      *\/     public int numSubarraysWithSum(int[] A, int S) {         \/\/ Write your code here.         if (A == null || A.length == 0) {             return 0;         }         int prefixSum = 0;         int res = 0;         \/\/ counts[i] means the number of prefixSum = i         int[] counts = new int[A.length + 1];         counts[0] = 1;         for (int i = 0; i &lt; A.length; i++) {             prefixSum += A[i];             if (prefixSum &gt;= S) {                 res += counts[prefixSum - S];             }             counts[prefixSum]++;         }         return res;     } }  <\/code><\/pre>\n<p>[\u66f4\u591a\u9898\u89e3\u53c2\u8003](https:\/\/www.jiuzhang.com\/solution\/binary-subarrays-with-sum\/?utm_source=sc-zhihuzl-sy )<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e5d\u7ae0\u7b97\u6cd5 | \u817e\u8baf\u9762\u8bd5\u9898\uff1a\u548c\u76f8\u540c\u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352293"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=352293"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352293\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=352293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352293"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}