{"id":777,"date":"2017-11-13T08:02:24","date_gmt":"2017-11-13T16:02:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=777"},"modified":"2018-04-19T08:40:08","modified_gmt":"2018-04-19T15:40:08","slug":"leetcode-724-find-pivot-index","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-724-find-pivot-index\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 724. Find Pivot Index"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 724. Find Pivot Index - \u5237\u9898\u627e\u5de5\u4f5c EP105\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/NPdBKCc-K5o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an array of integers\u00a0<code>nums<\/code>, write a method that returns the &#8220;pivot&#8221; index of this array.<\/p>\n<p>We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.<\/p>\n<p>If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \r\nnums = [1, 7, 3, 6, 5, 6]\r\nOutput: 3\r\nExplanation: \r\nThe sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.\r\nAlso, 3 is the first index where this occurs.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \r\nnums = [1, 2, 3]\r\nOutput: -1\r\nExplanation: \r\nThere is no index that satisfies the conditions in the problem statement.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>The length of\u00a0<code>nums<\/code>\u00a0will be in the range\u00a0<code>[0, 10000]<\/code>.<\/li>\n<li>Each element\u00a0<code>nums[i]<\/code>\u00a0will be an integer in the range\u00a0<code>[-1000, 1000]<\/code>.<\/li>\n<\/ul>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/724-ep105-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 43 ms\r\nclass Solution {\r\npublic:\r\n    int pivotIndex(vector&lt;int&gt;&amp; nums) {\r\n        const int sum = accumulate(nums.begin(), nums.end(), 0);\r\n        int l = 0;\r\n        int r = sum;\r\n        for (int i = 0; i &lt; nums.size(); ++i) {\r\n            r -= nums[i];\r\n            if (l == r) return i;\r\n            l += nums[i];\r\n        }\r\n        return -1;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Java<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 133 ms\r\nclass Solution {\r\n    public int pivotIndex(int[] nums) {\r\n        int sum = IntStream.of(nums).sum();\r\n        int l = 0;\r\n        int r = sum;\r\n        for (int i = 0; i &lt; nums.length; ++i) {\r\n            r -= nums[i];\r\n            if (l == r) return i;\r\n            l += nums[i];\r\n        }\r\n        return -1;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Python<\/p>\n<pre class=\"lang:default decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 112 ms\r\n\"\"\"\r\nclass Solution:\r\n    def pivotIndex(self, nums):\r\n        l, r = 0, sum(nums)\r\n        for i in range(len(nums)):\r\n            r -= nums[i]\r\n            if l == r: return i\r\n            l += nums[i]\r\n        return -1<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an array of integers\u00a0nums, write a method that returns the &#8220;pivot&#8221; index of this array. We define the pivot index as the index&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,163],"tags":[20,18,196],"class_list":["post-777","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-easy","tag-array","tag-dp","tag-pivot","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/777","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=777"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/777\/revisions"}],"predecessor-version":[{"id":2694,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/777\/revisions\/2694"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}