{"id":3095,"date":"2023-10-19T09:59:36","date_gmt":"2023-10-19T01:59:36","guid":{"rendered":"https:\/\/white-night.club\/?p=3095"},"modified":"2023-11-09T11:07:48","modified_gmt":"2023-11-09T03:07:48","slug":"ad76","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/19\/ad76\/","title":{"rendered":"1588.\u00a0\u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4\u00a0<code>arr<\/code>\u00a0\uff0c\u8bf7\u4f60\u8ba1\u7b97\u6240\u6709\u53ef\u80fd\u7684\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c\u3002<\/p>\n\n\n\n<p><strong>\u5b50\u6570\u7ec4<\/strong>&nbsp;\u5b9a\u4e49\u4e3a\u539f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b50\u5e8f\u5217\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u8fd4\u56de&nbsp;<code>arr<\/code>&nbsp;\u4e2d&nbsp;<strong>\u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,4,2,5,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>58\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u548c\u5b83\u4eec\u7684\u548c\u4e3a\uff1a\n[1] = 1\n[4] = 4\n[2] = 2\n[5] = 5\n[3] = 3\n[1,4,2] = 7\n[4,2,5] = 11\n[2,5,3] = 10\n[1,4,2,5,3] = 15\n\u6211\u4eec\u5c06\u6240\u6709\u503c\u6c42\u548c\u5f97\u5230 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n<strong>\u89e3\u91ca\uff1a<\/strong>\u603b\u5171\u53ea\u6709 2 \u4e2a\u957f\u5ea6\u4e3a\u5947\u6570\u7684\u5b50\u6570\u7ec4\uff0c[1] \u548c [2]\u3002\u5b83\u4eec\u7684\u548c\u4e3a 3 \u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>arr = [10,11,12]\n<strong>\u8f93\u51fa\uff1a<\/strong>66\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>1 &lt;= arr.length &lt;= 100<\/code><\/li>\n\n\n\n<li><code>1 &lt;= arr[i] &lt;= 1000<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u524d\u7f00\u548c<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u66b4\u529b\u505a\u8d77\u6765\u5f88\u9ebb\u70e6\u3002\u6c42\u6570\u7ec4\u8fde\u7eed\u5b50\u5e8f\u5217\u4e4b\u548c\uff0c\u90a3\u6211\u4eec\u5148\u8bd5\u8bd5\u524d\u7f00\u548c\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u662f\u6bcf\u4e2a\u5143\u7d20\u76f8\u52a0\uff0c\u8fd9\u4e2a\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u95ee\u9898\u5728\u4e8e\u5982\u4f55\u5904\u7406\u524d\u7f00\u548c\u3002\u6211\u4eec\u53ef\u4ee5\u4ece\u6837\u4f8b\u4e00\u5165\u624b\u3002\u4ecei=3\u5f00\u59cb\uff0c\u524d\u7f00\u548c\u7684\u89c4\u5f8b\u662f<\/p>\n\n\n\n<ul>\n<li>result=sum[3]-0<\/li>\n\n\n\n<li>result=sum[4]-sum[1]<\/li>\n\n\n\n<li>result=sum[5]-sum[2]<\/li>\n<\/ul>\n\n\n\n<p>\u5f53i=5\u65f6<\/p>\n\n\n\n<ul>\n<li>result=sum[5]-0<\/li>\n\n\n\n<li>result=sum[6]-sum[1] \uff08\u5982\u679c\u6709\u7684\u8bdd\uff09<\/li>\n\n\n\n<li>&#8230;<\/li>\n<\/ul>\n\n\n\n<p>\u90a3\u5199\u4e2a\u53ccfor\u5b8c\u4e8b\u4e86\u3002\u81f3\u4e8e\u8fdb\u9636\u91cc\u63d0\u5230\u7684O(n)\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u9274\u5b9a\u4e3a\u7eaf\u7eaf\u7684\u6570\u5b66\u9898\uff0c\u548c\u524d\u7f00\u548c\u6ca1\u5565\u5173\u7cfb\uff0c\u6240\u4ee5\u5c31\u4e0d\u770b\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    int sumOddLengthSubarrays(vector&lt;int>&amp; arr) {\r\n        int size=arr.size();\r\n        vector&lt;int>sum;\r\n        sum.resize(size+1);\r\n        int result=0;\r\n        for(int i=0;i&lt;size;i++)\r\n        {\r\n            sum&#91;i+1]=sum&#91;i]+arr&#91;i];\r\n            result+=arr&#91;i];\r\n        }\r\n        for(int i=3;i&lt;=size;i+=2)\r\n        {\r\n            for(int j=i;j&lt;=size;j++)\r\n            {\r\n                result+=sum&#91;j]-sum&#91;j-i];\r\n            }\r\n        }\r\n        return result;\r\n    }\r\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u524d\u7f00\u548c<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/3095"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=3095"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/3095\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=3095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=3095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=3095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}