{"id":10364,"date":"2025-04-16T20:19:33","date_gmt":"2025-04-17T03:19:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10364"},"modified":"2025-04-16T20:28:51","modified_gmt":"2025-04-17T03:28:51","slug":"leetcode-3517-smallest-palindromic-rearrangement-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-3517-smallest-palindromic-rearrangement-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3517. Smallest Palindromic Rearrangement I"},"content":{"rendered":"\n<p>\u8fd9\u9898\u8003\u67e5\u7684\u662f\u6392\u5e8f\u5427&#8230; \u8fd8\u6709rbegin\u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u666e\u901a\u6392\u5e8f\uff1a\u65f6\u95f4 O(nlogn) \/ \u7a7a\u95f4 O(1)<\/p>\n\n\n\n<p>\u524d\u534a\u90e8\u5206\u987a\u5e8f\u6392\u5e8f\uff0c\u540e\u534a\u90e8\u5206\u9006\u5e8f\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string smallestPalindrome(string s) {\n    const int n = s.length();\n    sort(begin(s), begin(s) + n \/ 2);\n    sort(rbegin(s), rbegin(s) + n \/ 2);\n    return s;\n  }\n};\n<\/pre>\n\n\n\n<p>\u8ba1\u6570\u6392\u5e8f\uff1a\u65f6\u95f4\uff1aO(n)\uff0c\u7a7a\u95f4\uff1aO(n) -> O(1)<\/p>\n\n\n\n<p>\u9996\u5c3e\u4e00\u8d77\u5199<\/p>\n\n\n\n<pre lang=\"c++\">\n\/\/ 7 ms, 54.65MB\nclass Solution {\npublic:\n  string smallestPalindrome(string s) {\n    vector<int> f(128);\n    for (size_t i = 0; i < s.size() \/ 2; ++i)\n      ++f[s[i]];\n    auto h=begin(s);\n    auto t=rbegin(s);\n    for (char c = 'a'; c <= 'z'; ++c)\n      while (f[c]--)\n        *h++ = *t++ = c;\n    return s;\n  }\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9898\u8003\u67e5\u7684\u662f\u6392\u5e8f\u5427&#8230; \u8fd8\u6709rbegin\u7684\u4f7f\u7528\u3002 \u666e\u901a\u6392\u5e8f\uff1a\u65f6\u95f4 O(nlogn) \/ \u7a7a\u95f4 O(1) \u524d\u534a\u90e8\u5206\u987a\u5e8f\u6392\u5e8f\uff0c\u540e\u534a\u90e8\u5206\u9006\u5e8f\u6392\u5e8f\u3002 class Solution { public: string smallestPalindrome(string s) { const int n = s.length(); sort(begin(s), begin(s) + n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[95,23,4],"class_list":["post-10364","post","type-post","status-publish","format-standard","hentry","category-string","tag-palindrome","tag-sort","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10364"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10364\/revisions"}],"predecessor-version":[{"id":10369,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10364\/revisions\/10369"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}