{"id":9592,"date":"2022-03-28T20:45:24","date_gmt":"2022-03-29T03:45:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9592"},"modified":"2022-03-28T20:46:24","modified_gmt":"2022-03-29T03:46:24","slug":"leetcode-2217-find-palindrome-with-fixed-length","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2217-find-palindrome-with-fixed-length\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2217. Find Palindrome With Fixed Length"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>queries<\/code>&nbsp;and a&nbsp;<strong>positive<\/strong>&nbsp;integer&nbsp;<code>intLength<\/code>, return&nbsp;<em>an array<\/em>&nbsp;<code>answer<\/code>&nbsp;<em>where<\/em>&nbsp;<code>answer[i]<\/code>&nbsp;<em>is either the&nbsp;<\/em><code>queries[i]<sup>th<\/sup><\/code>&nbsp;<em>smallest&nbsp;<strong>positive palindrome<\/strong>&nbsp;of length<\/em>&nbsp;<code>intLength<\/code>&nbsp;<em>or<\/em>&nbsp;<code>-1<\/code><em>&nbsp;if no such palindrome exists<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>palindrome<\/strong>&nbsp;is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> queries = [1,2,3,4,5,90], intLength = 3\n<strong>Output:<\/strong> [101,111,121,131,141,999]\n<strong>Explanation:<\/strong>\nThe first few palindromes of length 3 are:\n101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...\nThe 90<sup>th<\/sup> palindrome of length 3 is 999.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> queries = [2,4,6], intLength = 4\n<strong>Output:<\/strong> [1111,1331,1551]\n<strong>Explanation:<\/strong>\nThe first six palindromes of length 4 are:\n1001, 1111, 1221, 1331, 1441, and 1551.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= queries.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= queries[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= intLength&nbsp;&lt;= 15<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>For even length e.g. 4, we work with length \/ 2, e.g. 2. Numbers: 10, 11, 12, &#8230;, 99, starting from 10, ends with 99, which consist of 99 &#8211; 10 + 1 = 90 numbers. For the x-th number, e.g. 88, the left part is 10 + 88 &#8211; 1 = 97, just mirror it o get the palindrome. 97|79. Thus we can answer a query in O(k\/2) time which is critical.<\/p>\n\n\n\n<p><br>For odd length e.g. 3 we work with length \/ 2 + 1, e.g. 2, Numbers: 10, 11, 12, 99. Drop the last digit and mirror the left part to get the palindrome. 101, 111, 121, &#8230;, 999.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<long long> kthPalindrome(vector<int>& queries, int k) {\n    long long s = pow(10, (k + 1) \/ 2 - 1);\n    long long e = s * 10;\n    auto gen = [](long long x, bool isOdd) {\n      long long ans = x;\n      for (long long c = isOdd ? x \/ 10 : x; c; c \/= 10)\n        ans = ans * 10 + c % 10;\n      return ans;\n    };\n    vector<long long> ans;\n    for (int q : queries)\n      ans.push_back(q > e - s ? -1 : gen(s + q - 1, k & 1));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;queries&nbsp;and a&nbsp;positive&nbsp;integer&nbsp;intLength, return&nbsp;an array&nbsp;answer&nbsp;where&nbsp;answer[i]&nbsp;is either the&nbsp;queries[i]th&nbsp;smallest&nbsp;positive palindrome&nbsp;of length&nbsp;intLength&nbsp;or&nbsp;-1&nbsp;if no such palindrome exists. A&nbsp;palindrome&nbsp;is a number that reads the same backwards and forwards.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9592","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9592","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=9592"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9592\/revisions"}],"predecessor-version":[{"id":9595,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9592\/revisions\/9595"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}