<?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>baseball Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/baseball/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/baseball/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:41:16 +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>baseball Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/baseball/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 682. Baseball Game</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-682-baseball-game/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-682-baseball-game/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Sep 2017 06:12:43 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[baseball]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=395</guid>

					<description><![CDATA[<p>Problem: You&#8217;re now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one round&#8217;s&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-682-baseball-game/">花花酱 LeetCode 682. Baseball Game</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/8tQj4IuOzPk?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>You&#8217;re now a baseball game point recorder.</p>
<p>Given a list of strings, each string can be one of the 4 following types:</p>
<ol>
<li><code>Integer</code> (one round&#8217;s score): Directly represents the number of points you get in this round.</li>
<li><code>"+"</code> (one round&#8217;s score): Represents that the points you get in this round are the sum of the last two <code>valid</code>round&#8217;s points.</li>
<li><code>"D"</code> (one round&#8217;s score): Represents that the points you get in this round are the doubled data of the last <code>valid</code> round&#8217;s points.</li>
<li><code>"C"</code> (an operation, which isn&#8217;t a round&#8217;s score): Represents the last <code>valid</code> round&#8217;s points you get were invalid and should be removed.</li>
</ol>
<p>Each round&#8217;s operation is permanent and could have an impact on the round before and the round after.</p>
<p>You need to return the sum of the points you could get in all the rounds.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [&quot;5&quot;,&quot;2&quot;,&quot;C&quot;,&quot;D&quot;,&quot;+&quot;]
Output: 15
Explanation: 
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2's data was invalid. The sum is: 5.  
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [&quot;5&quot;,&quot;-2&quot;,&quot;4&quot;,&quot;C&quot;,&quot;D&quot;,&quot;9&quot;,&quot;+&quot;,&quot;+&quot;]
Output: 27
Explanation: 
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3's data is invalid. The sum is: 3.  
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.</pre><p><strong>N</strong><b>ote:</b></p>
<p>&nbsp;</p>
<ul>
<li>The size of the input list will be between 1 and 1000.</li>
<li>Every integer represented in the list will be between -30000 and 30000.</li>
</ul>
<p>&nbsp;</p>
<p><strong>Idea:</strong></p>
<p>Simulation</p>
<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>Solution:</strong></p>
<p>C++:</p><pre class="crayon-plain-tag">class Solution {
public:
    int calPoints(vector&lt;string&gt;&amp; ops) {
        vector&lt;int&gt; s;
        int sum = 0;
        for (const string&amp; op : ops) {
            if (op == "+")
                s.push_back(s.end()[-1] + s.end()[-2]);
            else if (op == "C")                
                s.pop_back();
            else if (op == "D")
                s.push_back(s.back() * 2);
            else
                s.push_back(stoi(op));
        }
        
        return accumulate(s.begin(), s.end(), 0);
    }
};</pre><p>&nbsp;</p>
<p>Java:</p><pre class="crayon-plain-tag">class Solution {
    public int calPoints(String[] ops) {
        List&lt;Integer&gt; s = new ArrayList&lt;Integer&gt;();
        
        for (String op : ops) {
            int n = s.size();            
            if (op.equals("+"))
                s.add(s.get(n - 1) + s.get(n - 2));
            else if (op.equals("C"))
                s.remove(n - 1);
            else if (op.equals("D"))
                s.add(s.get(n - 1) * 2);
            else
                s.add(Integer.parseInt(op));
        }

        return s.stream().reduce(0, Integer::sum);        
    }
}</pre><p>&nbsp;</p>
<p><strong>Python</strong></p><pre class="crayon-plain-tag">class Solution:
    def calPoints(self, ops):
        """
        :type ops: List[str]
        :rtype: int
        """
        s = []
        for op in ops:
            if op == "+": s += [s[-1] + s[-2]]
            elif op == "C": s = s[:-1]
            elif op == "D": s += [s[-1]*2]
            else: s += [int(op)]
        
        return sum(s)</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-682-baseball-game/">花花酱 LeetCode 682. Baseball Game</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/simulation/leetcode-682-baseball-game/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
