{"id":679,"date":"2020-06-22T13:14:11","date_gmt":"2020-06-22T05:14:11","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=679"},"modified":"2020-06-22T13:14:11","modified_gmt":"2020-06-22T05:14:11","slug":"jz5-%e7%94%a8%e4%b8%a4%e4%b8%aa%e6%a0%88%e5%ae%9e%e7%8e%b0%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=679","title":{"rendered":"JZ5 \u2014 \u7528\u4e24\u4e2a\u6808\u5b9e\u73b0\u961f\u5217"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u7528\u4e24\u4e2a\u6808\u6765\u5b9e\u73b0\u4e00\u4e2a\u961f\u5217\uff0c\u5b8c\u6210\u961f\u5217\u7684Push\u548cPop\u64cd\u4f5c\u3002 \u961f\u5217\u4e2d\u7684\u5143\u7d20\u4e3aint\u7c7b\u578b\u3002<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u6808\u662f\u5148\u8fdb\u540e\u51fa\uff0c\u961f\u5217\u662f\u5148\u8fdb\u5148\u51fa\u3002<br \/>\n\u5f88\u5bb9\u6613\u60f3\u5230\u5c06\u4e00\u4e2a\u6808\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u5f39\u51fa\u81f3\u53e6\u4e00\u4e2a\u6808\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5c06\u6808\u5e95\u53d8\u4e3a\u6808\u9876\u7684\u529f\u80fd\uff0c\u7136\u540e\u8fdb\u4e00\u6b65\u5b9e\u73b0\u5c06\u5148\u8fdb\u540e\u51fa\u53d8\u4e3a\u5148\u8fdb\u5148\u51fa\u3002<br \/>\n\u5047\u8bbe\u6709\u4e24\u4e2a\u6808<code>stack1<\/code>\u548c<code>stack2<\/code>\uff0c<code>stack1<\/code>\u8d1f\u8d23\u5b58\u50a8\u5916\u754c\u6570\u636e\uff0c<code>stack2<\/code>\u8d1f\u8d23\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa\u3002<br \/>\n\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u6b21\u4ece<code>stack1<\/code>\u5f39\u51fa\u6570\u636e\u5230<code>stack2<\/code>\u4e2d\uff0c\u5fc5\u987b\u4fdd\u8bc1<code>stack2<\/code>\u6b64\u65f6\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u4f1a\u628a\u4e4b\u524d\u7684\u5148\u8fdb\u6765\u7684\u6570\u636e\u538b\u5230\u961f\u4f0d\u7684\u540e\u9762\uff1b\u540c\u65f6\u5e94\u8be5\u4e00\u6b21\u6027\u5c06<code>stack1<\/code>\u4e2d\u7684\u6570\u636e\u5168\u90e8\u5f39\u51fa\uff0c\u56e0\u4e3a<code>stack1<\/code>\u6700\u5e95\u90e8\u7684\u6570\u636e\u662f\u6700\u5148\u8fdb\u6765\u7684\u3002<br \/>\n\u7efc\u4e0a\u6240\u8ff0\uff0c\u6b63\u786e\u7684\u7b56\u7565\u662f\uff1a\u5916\u90e8\u6267\u884c<code>push<\/code>\u64cd\u4f5c\u65f6\uff0c\u5c06\u6570\u636e\u6dfb\u52a0\u8fdb<code>stack1<\/code>\u4e2d\uff1b\u5916\u90e8\u6267\u884c<code>pop<\/code>\u64cd\u4f5c\u65f6\uff08\u5982\u679c<code>stack2<\/code>\u4e3a\u7a7a\u5219\u5148\u5c06<code>stack1<\/code>\u4e00\u6b21\u6027\u5168\u90e8\u5f39\u51fa\u81f3<code>stack2<\/code>\uff09\uff0c\u5c06<code>stack2<\/code>\u7684\u6808\u9876\u5f39\u51fa\u5373\u53ef\u3002<\/p>\n<h3>C++<\/h3>\n<pre><code class=\"language-cpp line-numbers\">class Solution\n{\npublic:\n    void push(int node)\n    {\n        stack1.push(node);\n    }\n\n    int pop()\n    {\n        if (stack2.empty())\n        {\n            while (!stack1.empty())\n            {\n                stack2.push(stack1.top());\n                stack1.pop();\n            }\n        }\n        int res = stack2.top();\n        stack2.pop();\n        return res;\n    }\n\nprivate:\n    stack&lt;int&gt; stack1;\n    stack&lt;int&gt; stack2;\n};\n<\/code><\/pre>\n<h3>Java<\/h3>\n<pre><code class=\"language-java line-numbers\">import java.util.Stack;\n\npublic class Solution {\n    Stack&lt;Integer&gt; stack1 = new Stack&lt;Integer&gt;();\n    Stack&lt;Integer&gt; stack2 = new Stack&lt;Integer&gt;();\n\n    public void push(int node) {\n        stack1.push(node);\n    }\n\n    public int pop() {\n        if (stack2.empty()) {\n            while (!stack1.empty()) {\n                stack2.push(stack1.pop());\n            }\n        }\n        return stack2.pop();\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7528\u4e24\u4e2a\u6808\u6765\u5b9e\u73b0\u4e00\u4e2a\u961f\u5217\uff0c\u5b8c\u6210\u961f\u5217\u7684Push\u548cPop\u64cd\u4f5c\u3002 \u961f\u5217\u4e2d\u7684\u5143\u7d20\u4e3aint\u7c7b\u578b\u3002 \u9898\u76ee\u5206\u6790 \u6808\u662f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[39],"class_list":["post-679","post","type-post","status-publish","format-standard","hentry","category-algo","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=679"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/679\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}