{"id":697,"date":"2020-09-26T11:12:00","date_gmt":"2020-09-26T03:12:00","guid":{"rendered":"http:\/\/39.96.58.60\/?p=697"},"modified":"2022-10-18T16:38:16","modified_gmt":"2022-10-18T08:38:16","slug":"leetcode-1588-%e6%89%80%e6%9c%89%e5%a5%87%e6%95%b0%e9%95%bf%e5%ba%a6%e5%ad%90%e6%95%b0%e7%bb%84%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=697","title":{"rendered":"Leetcode 1588. \u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4&nbsp;<code>arr<\/code>&nbsp;\uff0c\u8bf7\u4f60\u8ba1\u7b97\u6240\u6709\u53ef\u80fd\u7684\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c\u3002<\/p>\n<p><strong>\u5b50\u6570\u7ec4<\/strong> \u5b9a\u4e49\u4e3a\u539f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b50\u5e8f\u5217\u3002<\/p>\n<p>\u8bf7\u4f60\u8fd4\u56de <code>arr<\/code>&nbsp;\u4e2d <strong>\u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c<\/strong> \u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,4,2,5,3]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>58\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6240\u6709\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u548c\u5b83\u4eec\u7684\u548c\u4e3a\uff1a\n\n[1] = 1\n\n[4] = 4\n\n[2] = 2\n\n[5] = 5\n\n[3] = 3\n\n[1,4,2] = 7\n\n[4,2,5] = 11\n\n[2,5,3] = 10\n\n[1,4,2,5,3] = 15\n\n\u6211\u4eec\u5c06\u6240\u6709\u503c\u6c42\u548c\u5f97\u5230 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n\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<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [10,11,12]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>66\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= arr.length &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= arr[i] &lt;= 1000<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a72 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8656.70% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.4 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8625.43% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u4ee5\u5947\u6570\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u6c42\u548c\n\"\"\"\nclass Solution:\n    def sumOddLengthSubarrays(self, arr: List[int]) -&gt; int:\n        result = []\n        n = len(arr)\n\n        for l in range(1, n+1): # \u4f9d\u6b21\u5c1d\u8bd5 1~n\u7684\u957f\u5ea6\n            if l%2 == 1:    # \u5982\u679c\u5f53\u524d \u957f\u5ea6\u4e3a\u5947\u6570\n                for j in range(n-l+1):  # \u4ee5\u5f53\u524d\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u6c42\u548c\n                    result.append(sum(arr[j:j+l]))\n        return sum(result)\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a72 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8656.70% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8684.07% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u4ee5\u5947\u6570\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u6c42\u548c\n\"\"\"\nclass Solution:\n    def sumOddLengthSubarrays(self, arr: List[int]) -&gt; int:\n        result = []\n        n = len(arr)\n\n        for l in range(1, n+1, 2): # \u4f9d\u6b21\u5c1d\u8bd5 1~n\u7684\u957f\u5ea6\n            for j in range(n-l+1):  # \u4ee5\u5f53\u524d\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u6c42\u548c\n                result.append(sum(arr[j:j+l]))\n        return sum(result)\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4&nbsp;arr&nbsp;\uff0c\u8bf7\u4f60\u8ba1\u7b97\u6240\u6709\u53ef\u80fd\u7684\u5947\u6570\u957f\u5ea6\u5b50\u6570\u7ec4\u7684\u548c\u3002 \u5b50\u6570\u7ec4 \u5b9a\u4e49\u4e3a\u539f\u6570\u7ec4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/697"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=697"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":698,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions\/698"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}