{"id":445,"date":"2020-09-26T10:42:27","date_gmt":"2020-09-26T02:42:27","guid":{"rendered":"http:\/\/39.96.58.60\/?p=445"},"modified":"2022-10-18T16:39:15","modified_gmt":"2022-10-18T08:39:15","slug":"leetcode-1370-%e4%b8%8a%e5%8d%87%e4%b8%8b%e9%99%8d%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=445","title":{"rendered":"Leetcode 1370. \u4e0a\u5347\u4e0b\u964d\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\uff0c\u8bf7\u4f60\u6839\u636e\u4e0b\u9762\u7684\u7b97\u6cd5\u91cd\u65b0\u6784\u9020\u5b57\u7b26\u4e32\uff1a<\/p>\n<ol>\n<li>\u4ece <code>s<\/code>&nbsp;\u4e2d\u9009\u51fa <strong>\u6700\u5c0f<\/strong>&nbsp;\u7684\u5b57\u7b26\uff0c\u5c06\u5b83 <strong>\u63a5\u5728<\/strong>&nbsp;\u7ed3\u679c\u5b57\u7b26\u4e32\u7684\u540e\u9762\u3002<\/li>\n<li>\u4ece <code>s<\/code>&nbsp;\u5269\u4f59\u5b57\u7b26\u4e2d\u9009\u51fa&nbsp;<strong>\u6700\u5c0f<\/strong>&nbsp;\u7684\u5b57\u7b26\uff0c\u4e14\u8be5\u5b57\u7b26\u6bd4\u4e0a\u4e00\u4e2a\u6dfb\u52a0\u7684\u5b57\u7b26\u5927\uff0c\u5c06\u5b83 <strong>\u63a5\u5728<\/strong>&nbsp;\u7ed3\u679c\u5b57\u7b26\u4e32\u540e\u9762\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 2 \uff0c\u76f4\u5230\u4f60\u6ca1\u6cd5\u4ece <code>s<\/code>&nbsp;\u4e2d\u9009\u62e9\u5b57\u7b26\u3002<\/li>\n<li>\u4ece <code>s<\/code>&nbsp;\u4e2d\u9009\u51fa <strong>\u6700\u5927<\/strong>&nbsp;\u7684\u5b57\u7b26\uff0c\u5c06\u5b83 <strong>\u63a5\u5728<\/strong>&nbsp;\u7ed3\u679c\u5b57\u7b26\u4e32\u7684\u540e\u9762\u3002<\/li>\n<li>\u4ece <code>s<\/code>&nbsp;\u5269\u4f59\u5b57\u7b26\u4e2d\u9009\u51fa&nbsp;<strong>\u6700\u5927<\/strong>&nbsp;\u7684\u5b57\u7b26\uff0c\u4e14\u8be5\u5b57\u7b26\u6bd4\u4e0a\u4e00\u4e2a\u6dfb\u52a0\u7684\u5b57\u7b26\u5c0f\uff0c\u5c06\u5b83 <strong>\u63a5\u5728<\/strong>&nbsp;\u7ed3\u679c\u5b57\u7b26\u4e32\u540e\u9762\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 5&nbsp;\uff0c\u76f4\u5230\u4f60\u6ca1\u6cd5\u4ece <code>s<\/code>&nbsp;\u4e2d\u9009\u62e9\u5b57\u7b26\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 1 \u5230 6 \uff0c\u76f4\u5230 <code>s<\/code>&nbsp;\u4e2d\u6240\u6709\u5b57\u7b26\u90fd\u5df2\u7ecf\u88ab\u9009\u8fc7\u3002<\/li>\n<\/ol>\n<p>\u5728\u4efb\u4f55\u4e00\u6b65\u4e2d\uff0c\u5982\u679c\u6700\u5c0f\u6216\u8005\u6700\u5927\u5b57\u7b26\u4e0d\u6b62\u4e00\u4e2a&nbsp;\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8bf7\u4f60\u8fd4\u56de\u5c06&nbsp;<code>s<\/code>&nbsp;\u4e2d\u5b57\u7b26\u91cd\u65b0\u6392\u5e8f\u540e\u7684 <strong>\u7ed3\u679c\u5b57\u7b26\u4e32<\/strong> \u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"aaaabbbbcccc\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"abccbaabccba\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u7b2c\u4e00\u8f6e\u7684\u6b65\u9aa4 1\uff0c2\uff0c3 \u540e\uff0c\u7ed3\u679c\u5b57\u7b26\u4e32\u4e3a result = \"abc\"\n\n\u7b2c\u4e00\u8f6e\u7684\u6b65\u9aa4 4\uff0c5\uff0c6 \u540e\uff0c\u7ed3\u679c\u5b57\u7b26\u4e32\u4e3a result = \"abccba\"\n\n\u7b2c\u4e00\u8f6e\u7ed3\u675f\uff0c\u73b0\u5728 s = \"aabbcc\" \uff0c\u6211\u4eec\u518d\u6b21\u56de\u5230\u6b65\u9aa4 1\n\n\u7b2c\u4e8c\u8f6e\u7684\u6b65\u9aa4 1\uff0c2\uff0c3 \u540e\uff0c\u7ed3\u679c\u5b57\u7b26\u4e32\u4e3a result = \"abccbaabc\"\n\n\u7b2c\u4e8c\u8f6e\u7684\u6b65\u9aa4 4\uff0c5\uff0c6 \u540e\uff0c\u7ed3\u679c\u5b57\u7b26\u4e32\u4e3a result = \"abccbaabccba\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"rat\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"art\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5355\u8bcd \"rat\" \u5728\u4e0a\u8ff0\u7b97\u6cd5\u91cd\u6392\u5e8f\u4ee5\u540e\u53d8\u6210 \"art\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"leetcode\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"cdelotee\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"ggggggg\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"ggggggg\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"spo\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"ops\"\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= s.length &lt;= 500<\/code><\/li>\n<li><code>s<\/code>&nbsp;\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6392\u5e8f\u3001 \u5b57\u7b26\u4e32\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\uff1a84 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8661.57% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def:\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def sortString(self, s: str) -&gt; str:\n        s_list = list(s)    # \u5b57\u7b26\u4e32\u8f6c\u5217\u8868\n        result = []         # \u4fdd\u5b58\u6700\u7ec8\u7ed3\u679c\n        reverse = False     # \u7ffb\u8f6c\u987a\u5e8f\n        while s_list:\n            rs = list(set(s_list))  # \u53bb\u91cd\n            rs.sort(reverse=reverse)    # \u6392\u5e8f\n\n            result.extend(rs)   # \u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n            for r in rs:\n                s_list.remove(r)    # \u4ece\u5217\u8868\u4e2d\u79fb\u9664\u5df2\u7ecf\u6dfb\u52a0\u7684\u6570\u636e\n            reverse = not reverse   # \u7ffb\u8f6c\n\n        return ''.join(result)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;s&nbsp;\uff0c\u8bf7\u4f60\u6839\u636e\u4e0b\u9762\u7684\u7b97\u6cd5\u91cd\u65b0\u6784\u9020\u5b57\u7b26\u4e32\uff1a \u4ece s&nbsp;\u4e2d\u9009\u51fa \u6700\u5c0f&#038;&#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\/445"}],"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=445"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":446,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions\/446"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}