{"id":633,"date":"2020-09-26T11:05:36","date_gmt":"2020-09-26T03:05:36","guid":{"rendered":"http:\/\/39.96.58.60\/?p=633"},"modified":"2022-10-18T16:38:35","modified_gmt":"2022-10-18T08:38:35","slug":"leetcode-402-%e7%a7%bb%e6%8e%89k%e4%bd%8d%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=633","title":{"rendered":"Leetcode 402. \u79fb\u6389K\u4f4d\u6570\u5b57"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570&nbsp;<em>num<\/em>\uff0c\u79fb\u9664\u8fd9\u4e2a\u6570\u4e2d\u7684 <em>k <\/em>\u4f4d\u6570\u5b57\uff0c\u4f7f\u5f97\u5269\u4e0b\u7684\u6570\u5b57\u6700\u5c0f\u3002<\/p>\n<p><strong>\u6ce8\u610f:<\/strong><\/p>\n<ul>\n<li><em>num<\/em> \u7684\u957f\u5ea6\u5c0f\u4e8e 10002 \u4e14&nbsp;\u2265 <em>k\u3002<\/em><\/li>\n<li><em>num<\/em> \u4e0d\u4f1a\u5305\u542b\u4efb\u4f55\u524d\u5bfc\u96f6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b 1 :<\/strong><\/p>\n<pre>\u8f93\u5165: num = \"1432219\", k = 3\n\n\u8f93\u51fa: \"1219\"\n\n\u89e3\u91ca: \u79fb\u9664\u6389\u4e09\u4e2a\u6570\u5b57 4, 3, \u548c 2 \u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u6700\u5c0f\u7684\u6570\u5b57 1219\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2 :<\/strong><\/p>\n<pre>\u8f93\u5165: num = \"10200\", k = 1\n\n\u8f93\u51fa: \"200\"\n\n\u89e3\u91ca: \u79fb\u6389\u9996\u4f4d\u7684 1 \u5269\u4e0b\u7684\u6570\u5b57\u4e3a 200. \u6ce8\u610f\u8f93\u51fa\u4e0d\u80fd\u6709\u4efb\u4f55\u524d\u5bfc\u96f6\u3002\n\n<\/pre>\n<p>\u793a\u4f8b<strong> 3 :<\/strong><\/p>\n<pre>\u8f93\u5165: num = \"10\", k = 2\n\n\u8f93\u51fa: \"0\"\n\n\u89e3\u91ca: \u4ece\u539f\u6570\u5b57\u79fb\u9664\u6240\u6709\u7684\u6570\u5b57\uff0c\u5269\u4f59\u4e3a\u7a7a\u5c31\u662f0\u3002\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\u3001 \u8d2a\u5fc3\u7b97\u6cd5\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\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8669.82% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e866.73% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -&gt; str:\n        result = []\n        self.k = k\n        def insert(c):  # \u63d2\u5165c\u5b57\u7b26\n            # print(c, self.k, result)\n            if result == [] or self.k &lt; 1 or c &gt;= result[-1]:   # \u5982\u679cresult\u4e3a\u7a7a\uff0ck&lt;1\uff0c\u6216\u8005\u5f53\u524dc\u5927\u4e8e\u6808\u9876\u5143\u7d20\uff0c c\u5165\u6808\n                result.append(c)\n            else:   # \u5426\u5219\uff0c\u51fa\u6808\u4e00\u4e2a\u5b57\u7b26\uff0c\u91cd\u65b0\u5c1d\u8bd5\u63d2\u5165c\n                result.pop()\n                self.k -= 1\n                insert(c)\n\n        for c in num:   # \u4e00\u6b21\u63d2\u5165num\u4e2d\u6570\u5b57\n            if self.k &gt; 0:\n                insert(c)\n            else:\n                result.append(c)\n\n        for i in range(self.k): # \u5f53k&gt;1\u65f6\uff0c\u5982\u679c\u6808\u4e0d\u7a7a\uff0c\u5219\u7ee7\u7eed\u51fa\u6808\n            if result:\n                result.pop()\n        if result == []:\n            result = ['0']\n        return str(int(''.join(result)))\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570&nbsp;num\uff0c\u79fb\u9664\u8fd9\u4e2a\u6570\u4e2d\u7684 k \u4f4d\u6570\u5b57\uff0c\u4f7f\u5f97\u5269\u4e0b\u7684\u6570\u5b57\u6700\u5c0f\u3002 \u6ce8\u610f: n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","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\/633"}],"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=633"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":634,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/633\/revisions\/634"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}