<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>palindrom Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/palindrom/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/palindrom/</link>
	<description></description>
	<lastBuildDate>Sun, 21 Nov 2021 23:52:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>palindrom Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/palindrom/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2081. Sum of k-Mirror Numbers</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-2081-sum-of-k-mirror-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-2081-sum-of-k-mirror-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Nov 2021 23:51:32 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[palindrom]]></category>
		<category><![CDATA[search]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8758</guid>

					<description><![CDATA[<p>A&#160;k-mirror number&#160;is a&#160;positive&#160;integer&#160;without leading zeros&#160;that reads the same both forward and backward in base-10&#160;as well as&#160;in base-k. For example,&#160;9&#160;is a 2-mirror number. The representation of&#160;9&#160;in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-2081-sum-of-k-mirror-numbers/">花花酱 LeetCode 2081. Sum of k-Mirror Numbers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A&nbsp;<strong>k-mirror number</strong>&nbsp;is a&nbsp;<strong>positive</strong>&nbsp;integer&nbsp;<strong>without leading zeros</strong>&nbsp;that reads the same both forward and backward in base-10&nbsp;<strong>as well as</strong>&nbsp;in base-k.</p>



<ul><li>For example,&nbsp;<code>9</code>&nbsp;is a 2-mirror number. The representation of&nbsp;<code>9</code>&nbsp;in base-10 and base-2 are&nbsp;<code>9</code>&nbsp;and&nbsp;<code>1001</code>&nbsp;respectively, which read the same both forward and backward.</li><li>On the contrary,&nbsp;<code>4</code>&nbsp;is not a 2-mirror number. The representation of&nbsp;<code>4</code>&nbsp;in base-2 is&nbsp;<code>100</code>, which does not read the same both forward and backward.</li></ul>



<p>Given the base&nbsp;<code>k</code>&nbsp;and the number&nbsp;<code>n</code>, return&nbsp;<em>the&nbsp;<strong>sum</strong>&nbsp;of the</em>&nbsp;<code>n</code>&nbsp;<em><strong>smallest</strong>&nbsp;k-mirror numbers</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> k = 2, n = 5
<strong>Output:</strong> 25
<strong>Explanation:
</strong>The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
  base-10    base-2
    1          1
    3          11
    5          101
    7          111
    9          1001
Their sum = 1 + 3 + 5 + 7 + 9 = 25. 
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> k = 3, n = 7
<strong>Output:</strong> 499
<strong>Explanation:
</strong>The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
  base-10    base-3
    1          1
    2          2
    4          11
    8          22
    121        11111
    151        12121
    212        21212
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> k = 7, n = 17
<strong>Output:</strong> 20379000
<strong>Explanation:</strong> The 17 smallest 7-mirror numbers are:
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>2 &lt;= k &lt;= 9</code></li><li><code>1 &lt;= n &lt;= 30</code></li></ul>



<h2><strong>Solution: Generate palindromes in base-k.</strong></h2>



<div class="responsive-tabs">
<h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def kMirror(self, k: int, n: int) -&gt; int:
    def getNext(x: str) -&gt; str:
      s = list(x)
      l = len(s)    
      for i in range(l // 2, l):
        if int(s[i]) + 1 &gt;= k: continue
        s[i] = s[~i] = str(int(s[i]) + 1)
        for j in range(l // 2, i): s[j] = s[~j] = &quot;0&quot;
        return &quot;&quot;.join(s)
      return &quot;1&quot; + &quot;0&quot; * (l - 1) + &quot;1&quot;
    ans = 0
    x = &quot;0&quot;
    for _ in range(n):
      while True:
        x = getNext(x)
        val = int(x, k)
        if str(val) == str(val)[::-1]: break
      ans += val
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-2081-sum-of-k-mirror-numbers/">花花酱 LeetCode 2081. Sum of k-Mirror Numbers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-2081-sum-of-k-mirror-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
