<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pivot Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pivot/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pivot/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:40:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>pivot Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pivot/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 724. Find Pivot Index</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-724-find-pivot-index/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-724-find-pivot-index/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 13 Nov 2017 16:02:24 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[pivot]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=777</guid>

					<description><![CDATA[<p>Problem: Given an array of integers nums, write a method that returns the &#8220;pivot&#8221; index of this array. We define the pivot index as the index&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-724-find-pivot-index/">花花酱 LeetCode 724. Find Pivot Index</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/NPdBKCc-K5o?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given an array of integers <code>nums</code>, write a method that returns the &#8220;pivot&#8221; index of this array.</p>
<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>
<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>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The 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.
Also, 3 is the first index where this occurs.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.</pre><p><b>Note:</b></p>
<p>&nbsp;</p>
<ul>
<li>The length of <code>nums</code> will be in the range <code>[0, 10000]</code>.</li>
<li>Each element <code>nums[i]</code> will be an integer in the range <code>[-1000, 1000]</code>.</li>
</ul>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<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 />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>DP</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/724-ep105.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 43 ms
class Solution {
public:
    int pivotIndex(vector&lt;int&gt;&amp; nums) {
        const int sum = accumulate(nums.begin(), nums.end(), 0);
        int l = 0;
        int r = sum;
        for (int i = 0; i &lt; nums.size(); ++i) {
            r -= nums[i];
            if (l == r) return i;
            l += nums[i];
        }
        return -1;
    }
};</pre><p>&nbsp;</p>
<p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 133 ms
class Solution {
    public int pivotIndex(int[] nums) {
        int sum = IntStream.of(nums).sum();
        int l = 0;
        int r = sum;
        for (int i = 0; i &lt; nums.length; ++i) {
            r -= nums[i];
            if (l == r) return i;
            l += nums[i];
        }
        return -1;
    }
}</pre><p>&nbsp;</p>
<p>Python</p><pre class="crayon-plain-tag">"""
Author: Huahua
Runtime: 112 ms
"""
class Solution:
    def pivotIndex(self, nums):
        l, r = 0, sum(nums)
        for i in range(len(nums)):
            r -= nums[i]
            if l == r: return i
            l += nums[i]
        return -1</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-724-find-pivot-index/">花花酱 LeetCode 724. Find Pivot Index</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-724-find-pivot-index/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
