{"id":395,"date":"2017-09-23T23:12:43","date_gmt":"2017-09-24T06:12:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=395"},"modified":"2018-04-19T08:41:16","modified_gmt":"2018-04-19T15:41:16","slug":"leetcode-682-baseball-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-682-baseball-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 682. Baseball Game"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 682. Baseball Game - \u5237\u9898\u627e\u5de5\u4f5c EP69\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/8tQj4IuOzPk?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>You&#8217;re now a baseball game point recorder.<\/p>\n<p>Given a list of strings, each string can be one of the 4 following types:<\/p>\n<ol>\n<li><code>Integer<\/code>\u00a0(one round&#8217;s score): Directly represents the number of points you get in this round.<\/li>\n<li><code>\"+\"<\/code>\u00a0(one round&#8217;s score): Represents that the points you get in this round are the sum of the last two\u00a0<code>valid<\/code>round&#8217;s points.<\/li>\n<li><code>\"D\"<\/code>\u00a0(one round&#8217;s score): Represents that the points you get in this round are the doubled data of the last\u00a0<code>valid<\/code>\u00a0round&#8217;s points.<\/li>\n<li><code>\"C\"<\/code>\u00a0(an operation, which isn&#8217;t a round&#8217;s score): Represents the last\u00a0<code>valid<\/code>\u00a0round&#8217;s points you get were invalid and should be removed.<\/li>\n<\/ol>\n<p>Each round&#8217;s operation is permanent and could have an impact on the round before and the round after.<\/p>\n<p>You need to return the sum of the points you could get in all the rounds.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: [\"5\",\"2\",\"C\",\"D\",\"+\"]\r\nOutput: 15\r\nExplanation: \r\nRound 1: You could get 5 points. The sum is: 5.\r\nRound 2: You could get 2 points. The sum is: 7.\r\nOperation 1: The round 2's data was invalid. The sum is: 5.  \r\nRound 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.\r\nRound 4: You could get 5 + 10 = 15 points. The sum is: 30.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: [\"5\",\"-2\",\"4\",\"C\",\"D\",\"9\",\"+\",\"+\"]\r\nOutput: 27\r\nExplanation: \r\nRound 1: You could get 5 points. The sum is: 5.\r\nRound 2: You could get -2 points. The sum is: 3.\r\nRound 3: You could get 4 points. The sum is: 7.\r\nOperation 1: The round 3's data is invalid. The sum is: 3.  \r\nRound 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.\r\nRound 5: You could get 9 points. The sum is: 8.\r\nRound 6: You could get -4 + 9 = 5 points. The sum is 13.\r\nRound 7: You could get 9 + 5 = 14 points. The sum is 27.\r\n<\/pre>\n<p><strong>N<\/strong><b>ote:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>The size of the input list will be between 1 and 1000.<\/li>\n<li>Every integer represented in the list will be between -30000 and 30000.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Simulation<\/p>\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>Solution:<\/strong><\/p>\n<p>C++:<\/p>\n<pre class=\"lang:c++ decode:true\">class Solution {\r\npublic:\r\n    int calPoints(vector&lt;string&gt;&amp; ops) {\r\n        vector&lt;int&gt; s;\r\n        int sum = 0;\r\n        for (const string&amp; op : ops) {\r\n            if (op == \"+\")\r\n                s.push_back(s.end()[-1] + s.end()[-2]);\r\n            else if (op == \"C\")                \r\n                s.pop_back();\r\n            else if (op == \"D\")\r\n                s.push_back(s.back() * 2);\r\n            else\r\n                s.push_back(stoi(op));\r\n        }\r\n        \r\n        return accumulate(s.begin(), s.end(), 0);\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Java:<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\n    public int calPoints(String[] ops) {\r\n        List&lt;Integer&gt; s = new ArrayList&lt;Integer&gt;();\r\n        \r\n        for (String op : ops) {\r\n            int n = s.size();            \r\n            if (op.equals(\"+\"))\r\n                s.add(s.get(n - 1) + s.get(n - 2));\r\n            else if (op.equals(\"C\"))\r\n                s.remove(n - 1);\r\n            else if (op.equals(\"D\"))\r\n                s.add(s.get(n - 1) * 2);\r\n            else\r\n                s.add(Integer.parseInt(op));\r\n        }\r\n\r\n        return s.stream().reduce(0, Integer::sum);        \r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"lang:python decode:true \">class Solution:\r\n    def calPoints(self, ops):\r\n        \"\"\"\r\n        :type ops: List[str]\r\n        :rtype: int\r\n        \"\"\"\r\n        s = []\r\n        for op in ops:\r\n            if op == \"+\": s += [s[-1] + s[-2]]\r\n            elif op == \"C\": s = s[:-1]\r\n            elif op == \"D\": s += [s[-1]*2]\r\n            else: s += [int(op)]\r\n        \r\n        return sum(s)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<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\u00a0(one round&#8217;s&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,105],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-baseball","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/395","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=395"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":2711,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/395\/revisions\/2711"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}