{"id":6543,"date":"2018-03-29T17:09:22","date_gmt":"2018-03-29T09:09:22","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6543"},"modified":"2018-04-01T14:48:09","modified_gmt":"2018-04-01T06:48:09","slug":"%e6%a0%88%e3%80%81%e6%a0%91%e7%9b%b8%e5%85%b3%e7%ae%97%e6%b3%95%e9%97%ae%e9%a2%98%e9%9b%86%e9%94%a6","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6543.html","title":{"rendered":"\u6808\u3001\u6811\u76f8\u5173\u7b97\u6cd5\u95ee\u9898\u96c6\u9526"},"content":{"rendered":"<h1>\u9012\u5f52\u53cd\u8f6c\u6808\u7684\u987a\u5e8f<\/h1>\n<p>\u8981\u6c42\u53ea\u4f7f\u7528\u5e38\u6570\u91cf\u4e2a\u53d8\u91cf<\/p>\n<p>\u6211\u4eec\u8981\u53cd\u8f6c\u4e00\u4e2a\u6808\uff0c\u5982\u679c\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u6808\u4f5c\u4e3a\u8f85\u52a9\u7684\u8bdd\uff0c\u90a3\u4e48\u53cd\u8f6c\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4e00\u4e2a\u63a5\u4e00\u4e2apush\u5230\u8f85\u52a9\u6808\u91cc\u518dpush\u56de\u6765\u5c31\u884c\u4e86\u3002\u90a3\u4e48\u5047\u5982\u4e0d\u80fd\u4f7f\u7528\u8f85\u52a9\u6808\uff0c\u6570\u7ec4\u7b49\u7a7a\u95f4\u4e3aO(n)\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ea\u4f7f\u7528O\uff081\uff09\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u5373\u53ea\u80fd\u6709\u5e38\u6570\u4e2a\u53d8\u91cf\uff0c\u600e\u4e48\u5b9e\u73b0\u5c06\u6808\u53cd\u8f6c\uff1f\u5373\u539f\u6765\u7684\u6808\u9876\u5728\u6808\u5e95\uff0c\u6808\u5e95\u53d8\u6210\u6808\u9876\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\u5982\u56fe\u793a\u4f8b<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6544\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352.png\" alt=\"\" width=\"1015\" height=\"342\" srcset=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352.png 1015w, https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352-300x101.png 300w, https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352-768x259.png 768w, https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/03\/151329074634352-624x210.png 624w\" sizes=\"auto, (max-width: 1015px) 100vw, 1015px\" \/><\/a><\/p>\n<ul>\n<li>\u9996\u5148\u5047\u8bbe\u6808\u91cc\u9762\u4ece\u6808\u5e95\u5230\u6808\u9876\u5b58\u50a8\u7684\u4f9d\u6b21\u662f 1 2 3 4 5,\u6211\u4eec\u53cd\u8f6c\u4ee5\u540e\u5e0c\u671b\u5f97\u5230\u7684\u7ed3\u679c\u662f 5 4 3 2 1.<\/li>\n<li>\u7b2c\u4e00\u6b65pop\u51fa\u6765\u7b2c\u4e00\u4e2a\u65705\uff0c\u5b58\u5230temp1\u4e2d\uff0c\u6b64\u65f6\u7684\u6808\u4e2d\u7531\u4e0b\u5230\u4e0a\u662f1 2 3 4\u3002<\/li>\n<li>\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u4e0d\u7528\u8003\u8651\u5177\u4f53\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u9012\u5f52\u8c03\u7528\u7ed3\u675f\u540e\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4f7f\u6808\u4e2d\u7684\u5143\u7d20\u53d8\u6210\u4e86\u7531\u4e0b\u5230\u4e0a 4 3 2 1\uff08\u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u672c\u8eab\u7684\u610f\u601d\u5c31\u662f\u53cd\u8f6c\u6808\uff09\u3002<\/li>\n<li>\u6b64\u65f6pop\u51fa\u6765\u7b2c\u4e8c\u4e2a\u65701 \uff0c\u5b58\u5230temp2\u4e2d\uff0c\u6b64\u65f6\u7684\u6808\u4e2d\u7531\u4e0b\u5230\u4e0a\u662f 4 3 2\u3002<\/li>\n<li>\u518d\u9012\u5f52\u8c03\u7528\u672c\u8eab\uff0c\u62bd\u8c61\u8003\u8651\u8fd9\u4e2a\u9012\u5f52\u5b8c\u6210\u540e\u5f97\u5230\u7684\u7ed3\u679c\u662f\u6808\u4e2d\u7684\u503c\u53d8\u6210\u4e86 2 3 4\u3002<\/li>\n<li>\u63a5\u7740\u6211\u4eec\u628apush\uff08temp1\uff09\uff0c\u6808\u53d8\u6210\u4e86 2 3 4 5\u3002<\/li>\n<li>\u63a5\u7740\u9012\u5f52\u8c03\u7528\u672c\u8eab\uff0c\u9012\u5f52\u5b8c\u6210\u540e\u6808\u53d8\u6210\u4e86 5 4 3 2\u3002<\/li>\n<li>\u63a5\u7740push\uff08temp2\uff09\u3002\u597d\u4e86\uff0c\u6808\u53d8\u6210\u4e86 5 4 3 2 1\uff0c\u53cd\u8f6c\u5b8c\u6210\uff01<\/li>\n<li>\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u8003\u8651\u9012\u5f52\u51fd\u6570\u7ed3\u675f\u7684\u6761\u4ef6\u4e86\uff0c\u8be5\u51fd\u6570\u7684\u7ed3\u675f\u6761\u4ef6\u662f\u5f53\u6808\u4e3a\u7a7a\u6216\u8005\u6808\u91cc\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u65f6\u5019\uff0creturn\u3002<\/li>\n<\/ul>\n<p>\u5982\u4f55\u77e5\u9053\u6808\u91cc\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5148\u8bb0\u5f55\u4e0b\u6808\u9876\u5143\u7d20\uff0c\u7136\u540epop\uff0c\u5982\u679c\u6808\u53d8\u6210\u7a7a\uff0c\u5219\u8bf4\u660e\u662f\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0cpush\u56de\u53bb\u518dreturn\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\ndef reverse_stack(stack):\r\n    if len(stack) == 0:\r\n        return\r\n    # \u5982\u679cs\u91cc\u9762\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5c31\u8fd4\u56de\uff0c\u5426\u5219\u5c31\u4e0d\u8fd4\u56de\u3002\u5177\u4f53\u5b9e\u73b0\u662f\u5148pop\u51fa\u6765\u4e00\u4e2a\uff0c\u5224\u65ad\u5269\u4e0b\u7684\u662f\u4e0d\u662f\u7a7a\u6808\u3002\r\n    topa = stack.pop()\r\n    if len(stack) == 0:\r\n        stack.append(topa)\r\n        return\r\n    stack.append(topa)\r\n    temp1 = stack.pop()\r\n    reverse_stack(stack)\r\n\r\n    temp2 = stack.pop()\r\n    reverse_stack(stack)\r\n    stack.append(temp1)\r\n    reverse_stack(stack)\r\n    stack.append(temp2)\r\n\r\nif __name__ == '__main__':\r\n    arr = &#x5B;1, 3, 1, 4, 5, 3, 6, 2]\r\n    reverse_stack(arr)\r\n    assert arr == &#x5B;2, 6, 3, 5, 4, 1, 3, 1]\r\n\r\n<\/pre>\n<h1>\u652f\u6301\u83b7\u53d6\u6700\u5c0f\u503c\u7684\u6808<\/h1>\n<p>\u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u6808\uff0c\u9664pop\u4e0epush\u65b9\u6cd5\uff0c\u8fd8\u652f\u6301min\u65b9\u6cd5\uff0c\u53ef\u8fd4\u56de\u6808\u5143\u7d20\u4e2d\u7684\u6700\u5c0f\u503c\u3002push\u3001pop\u548cmin\u4e09\u4e2a\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5fc5\u987b\u4e3aO(1)<\/p>\n<p>\u8fd9\u662fLeetCode\u4e0a\u9762\u7684\u4e00\u9053\u9898\u76ee\uff0chttps:\/\/leetcode.com\/problems\/min-stack\/description\/<\/p>\n<p>\u601d\u8def1\uff1a\u5c06\u6bcf\u6b21\u5165\u6808\u7684\u5143\u7d20\u53ca\u5f53\u524d\u6700\u5c0f\u503c\u4e00\u8d77\u5b58\u8fdb\u53bb\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass MinStack(object):\r\n\r\n    def __init__(self):\r\n        self.stack = &#x5B;]\r\n\r\n    def push(self, x):\r\n        cur_min = self.getMin() \r\n        cur_min = cur_min if cur_min!=None else sys.maxint\r\n        self.stack.append((x, min(cur_min, x)))\r\n\r\n    def pop(self):\r\n        self.stack.pop() \r\n\r\n    def top(self):\r\n        if self.stack:\r\n            return self.stack&#x5B;-1]&#x5B;0]\r\n        return None \r\n\r\n    def getMin(self):\r\n        if self.stack:\r\n            return self.stack&#x5B;-1]&#x5B;1]\r\n        return None\r\n<\/pre>\n<p>\u601d\u8def2\uff1a\u53ef\u4ee5\u5229\u7528\u989d\u5916\u7684\u6808\u6765\u8bb0\u5f55\u6700\u5c0f\u503c\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u7a7a\u95f4\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sys\r\n\r\nclass MinStack(object):\r\n\r\n    def __init__(self):\r\n        self.stack = &#x5B;]\r\n        self.mins = &#x5B;]\r\n\r\n    def _min(self):\r\n        if self.mins:\r\n            return self.mins&#x5B;-1]\r\n        return sys.maxint\r\n\r\n    def push(self, x):\r\n        self.stack.append(x)\r\n        if x &lt;= self._min():\r\n            self.mins.append(x)\r\n\r\n    def pop(self):\r\n        v = self.stack.pop() \r\n        if v == self._min():\r\n            self.mins.pop()\r\n        return v\r\n\r\n    def top(self):\r\n        if self.stack:\r\n            return self.stack&#x5B;-1]\r\n        return None \r\n\r\n    def getMin(self):\r\n        return self._min()\r\n<\/pre>\n<h1>\u6309\u5347\u5e8f\u5bf9\u6808\u8fdb\u884c\u6392\u5e8f<\/h1>\n<p>\u7f16\u5199\u7a0b\u5e8f\uff0c\u6309\u5347\u5e8f\u5bf9\u6808\u8fdb\u884c\u6392\u5e8f\uff08\u5373\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u6808\u9876\uff09\u3002\u6700\u591a\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u6808\u5b58\u653e\u4e34\u65f6\u6570\u636e\uff0c\u4f46\u4e0d\u5f97\u5c06\u5143\u7d20\u590d\u5236\u5230\u522b\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff08\u5982\u6570\u7ec4\uff09\u3002\u8be5\u6808\u652f\u6301\u5982\u4e0b\u64cd\u4f5c\uff1apush\u3001pop\u3001peek\u548cisEmpty\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u5982\u4e0b\u4e24\u4e2a\u6808\uff0c\u5176\u4e2ds2\u662f\u201c\u6392\u5e8f\u7684\u201d\uff0cs1\u5219\u662f\u672a\u6392\u5e8f\u7684\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns1 = &#x5B;5, 10, 7]\r\ns2 = &#x5B;12, 8, 3, 1]\r\n<\/pre>\n<p>\u4eces1\u4e2d\u5f39\u51fa5\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5728s2\u4e2d\u627e\u4e2a\u5408\u9002\u7684\u4f4d\u7f6e\u63d2\u5165\u8fd9\u4e2a\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u786e\u4f4d\u7f6e\u662f\u5728s2\u5143\u7d203\u4e4b\u4e0a\u3002\u600e\u6837\u624d\u80fd\u5c065\u63d2\u5165\u90a3\u4e2a\u4f4d\u7f6e\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u5148\u4eces1\u4e2d\u5f39\u51fa5\uff0c\u5c06\u5176\u5b58\u653e\u5728\u4e34\u65f6\u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c\u5c0612\u548c8\u79fb\u81f3s1\uff08\u4eces2\u4e2d\u5f39\u51fa\u8fd9\u4e24\u4e2a\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u538b\u5165s1\u4e2d\uff09\uff0c\u7136\u540e\u5c065\u538b\u5165s2\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\u6b65\u9aa41\r\ns1 = &#x5B;10, 7]\r\ns2 = &#x5B;12, 8, 3, 1]\r\ntmp = 5\r\n\r\n\u6b65\u9aa42\r\ns1 = &#x5B;8, 12, 10, 7]\r\ns2 = &#x5B;3, 1]\r\ntmp = 5\r\n\r\n\u6b65\u9aa43\r\ns1 = &#x5B;8, 12, 10, 7]\r\ns2 = &#x5B;5, 3, 1]\r\ntmp = --\r\n<\/pre>\n<p>\u6ce8\u610f\uff0c8\u548c12\u4ecd\u5728s1\u4e2d\uff0c\u8fd9\u6ca1\u5173\u7cfb\uff01\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u5904\u74065\u90a3\u6837\u91cd\u590d\u76f8\u5173\u6b65\u9aa4\uff0c\u6bcf\u6b21\u5f39\u51fas1\u6808\u9876\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5165s2\u4e2d\u7684\u5408\u9002\u4f4d\u7f6e\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef stack_sort(s):\r\n    s2 = &#x5B;]\r\n    while s:\r\n        tmp = s.pop()\r\n        while s2 and s2&#x5B;-1] &gt; tmp:\r\n            s.append(s2.pop())\r\n        s2.append(tmp)\r\n    return s2\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9012\u5f52\u53cd\u8f6c\u6808\u7684\u987a\u5e8f \u8981\u6c42\u53ea\u4f7f\u7528\u5e38\u6570\u91cf\u4e2a\u53d8\u91cf \u6211\u4eec\u8981\u53cd\u8f6c\u4e00\u4e2a\u6808\uff0c\u5982\u679c\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u6808\u4f5c\u4e3a\u8f85\u52a9\u7684\u8bdd\uff0c\u90a3\u4e48\u53cd\u8f6c\u8d77\u6765\u5f88\u7b80\u5355 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-6543","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6543"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6543\/revisions"}],"predecessor-version":[{"id":6547,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6543\/revisions\/6547"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}