{"id":413,"date":"2020-09-26T10:38:42","date_gmt":"2020-09-26T02:38:42","guid":{"rendered":"http:\/\/39.96.58.60\/?p=413"},"modified":"2022-10-18T16:39:24","modified_gmt":"2022-10-18T08:39:24","slug":"leetcode-150-%e9%80%86%e6%b3%a2%e5%85%b0%e8%a1%a8%e8%be%be%e5%bc%8f%e6%b1%82%e5%80%bc","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=413","title":{"rendered":"Leetcode 150. \u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u6c42\u503c"},"content":{"rendered":"<p>\u6839\u636e<a href=\"https:\/\/baike.baidu.com\/item\/%E9%80%86%E6%B3%A2%E5%85%B0%E5%BC%8F\/128437\" target=\"_blank\" rel=\"noopener noreferrer\"> \u9006\u6ce2\u5170\u8868\u793a\u6cd5<\/a>\uff0c\u6c42\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n<p>\u6709\u6548\u7684\u8fd0\u7b97\u7b26\u5305\u62ec&nbsp;<code>+<\/code>,&nbsp;<code>-<\/code>,&nbsp;<code>*<\/code>,&nbsp;<code>\/<\/code>&nbsp;\u3002\u6bcf\u4e2a\u8fd0\u7b97\u5bf9\u8c61\u53ef\u4ee5\u662f\u6574\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6574\u6570\u9664\u6cd5\u53ea\u4fdd\u7559\u6574\u6570\u90e8\u5206\u3002<\/li>\n<li>\u7ed9\u5b9a\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u603b\u662f\u6709\u6548\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8868\u8fbe\u5f0f\u603b\u4f1a\u5f97\u51fa\u6709\u6548\u6570\u503c\u4e14\u4e0d\u5b58\u5728\u9664\u6570\u4e3a 0 \u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [\"2\", \"1\", \"+\", \"3\", \"*\"]\n\n<strong>\u8f93\u51fa:<\/strong> 9\n\n<strong>\u89e3\u91ca:<\/strong> \u8be5\u7b97\u5f0f\u8f6c\u5316\u4e3a\u5e38\u89c1\u7684\u4e2d\u7f00\u7b97\u672f\u8868\u8fbe\u5f0f\u4e3a\uff1a((2 + 1) * 3) = 9\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [\"4\", \"13\", \"5\", \"\/\", \"+\"]\n\n<strong>\u8f93\u51fa:<\/strong> 6\n\n<strong>\u89e3\u91ca:<\/strong> \u8be5\u7b97\u5f0f\u8f6c\u5316\u4e3a\u5e38\u89c1\u7684\u4e2d\u7f00\u7b97\u672f\u8868\u8fbe\u5f0f\u4e3a\uff1a(4 + (13 \/ 5)) = 6\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [\"10\", \"6\", \"9\", \"3\", \"+\", \"-11\", \"*\", \"\/\", \"*\", \"17\", \"+\", \"5\", \"+\"]\n\n<strong>\u8f93\u51fa:<\/strong> 22\n\n<strong>\u89e3\u91ca:<\/strong> \n\n\u8be5\u7b97\u5f0f\u8f6c\u5316\u4e3a\u5e38\u89c1\u7684\u4e2d\u7f00\u7b97\u672f\u8868\u8fbe\u5f0f\u4e3a\uff1a\n\n  ((10 * (6 \/ ((9 + 3) * -11))) + 17) + 5\n\n= ((10 * (6 \/ (12 * -11))) + 17) + 5\n\n= ((10 * (6 \/ -132)) + 17) + 5\n\n= ((10 * 0) + 17) + 5\n\n= (0 + 17) + 5\n\n= 17 + 5\n\n= 22<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\uff1a<\/strong><\/p>\n<p>\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u540e\u7f00\u8868\u8fbe\u5f0f\uff0c\u6240\u8c13\u540e\u7f00\u5c31\u662f\u6307\u7b97\u7b26\u5199\u5728\u540e\u9762\u3002<\/p>\n<ul>\n<li>\u5e73\u5e38\u4f7f\u7528\u7684\u7b97\u5f0f\u5219\u662f\u4e00\u79cd\u4e2d\u7f00\u8868\u8fbe\u5f0f\uff0c\u5982 <code>( 1 + 2 ) * ( 3 + 4 )<\/code> \u3002<\/li>\n<li>\u8be5\u7b97\u5f0f\u7684\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u5199\u6cd5\u4e3a <code>( ( 1 2 + ) ( 3 4 + ) * )<\/code> \u3002<\/li>\n<\/ul>\n<p>\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e24\u4e2a\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u53bb\u6389\u62ec\u53f7\u540e\u8868\u8fbe\u5f0f\u65e0\u6b67\u4e49\uff0c\u4e0a\u5f0f\u5373\u4fbf\u5199\u6210 <code>1 2 + 3 4 + * <\/code>\u4e5f\u53ef\u4ee5\u4f9d\u636e\u6b21\u5e8f\u8ba1\u7b97\u51fa\u6b63\u786e\u7ed3\u679c\u3002<\/li>\n<li>\u9002\u5408\u7528\u6808\u64cd\u4f5c\u8fd0\u7b97\uff1a\u9047\u5230\u6570\u5b57\u5219\u5165\u6808\uff1b\u9047\u5230\u7b97\u7b26\u5219\u53d6\u51fa\u6808\u9876\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u538b\u5165\u6808\u4e2d\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\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\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.20% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.27% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6808\n    \u5f53\u9047\u5230+-*\/\u65f6\uff0c\u51fa\u6808\u4e24\u4e2a\uff0c\u8fdb\u884c\u8ba1\u7b97\u540e\uff0c\u5c06\u7ed3\u679c\u5165\u6808\n\"\"\"\nclass Solution:\n    def evalRPN(self, tokens: List[str]) -&gt; int:\n        record = []\n        for token in tokens:\n            if token == '+':\n                b = record.pop()    # \u51fa\u6808\u4e24\u4e2a\u6570\u5b57\uff0c\u5206\u522b\u4f5c\u4e3aa,b\n                a = record.pop()\n                cal = int(int(a) + int(b))  # \u8ba1\u7b97\u83b7\u5f97\u7ed3\u679c\uff0c\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u5165\u6808\n                record.append(cal)\n            elif token == '-':\n                b = record.pop()\n                a = record.pop()\n                cal = int(int(a) - int(b))\n                record.append(cal)\n            elif token == '*':\n                b = record.pop()\n                a = record.pop()\n                cal = int(int(a) * int(b))\n                record.append(cal)\n            elif token == '\/':\n                b = record.pop()\n                a = record.pop()\n                cal = int(int(a) \/ int(b))\n                record.append(cal)\n            else:\n                record.append(token)\n        return int(record.pop())<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u636e \u9006\u6ce2\u5170\u8868\u793a\u6cd5\uff0c\u6c42\u8868\u8fbe\u5f0f\u7684\u503c\u3002 \u6709\u6548\u7684\u8fd0\u7b97\u7b26\u5305\u62ec&nbsp;+,&nbsp;-,&nbsp;*,&#038;nbsp&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/413"}],"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=413"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/413\/revisions"}],"predecessor-version":[{"id":414,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/413\/revisions\/414"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}