1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-12 08:55:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/统计二进制回文数字的数目(English) [count-binary-palindromic-numbers].html
2025-09-25 00:20:19 +08:00

51 lines
1.8 KiB
HTML

<p>You are given a <strong>non-negative</strong> integer <code>n</code>.</p>
<p>A <strong>non-negative</strong> integer is called <strong>binary-palindromic</strong> if its binary representation (written without leading zeros) reads the same forward and backward.</p>
<p>Return the number of integers <code><font face="monospace">k</font></code> such that <code>0 &lt;= k &lt;= n</code> and the binary representation of <code><font face="monospace">k</font></code> is a palindrome.</p>
<p><strong>Note:</strong> The number 0 is considered binary-palindromic, and its representation is <code>&quot;0&quot;</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 9</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The integers <code>k</code> in the range <code>[0, 9]</code> whose binary representations are palindromes are:</p>
<ul>
<li><code>0 &rarr; &quot;0&quot;</code></li>
<li><code>1 &rarr; &quot;1&quot;</code></li>
<li><code>3 &rarr; &quot;11&quot;</code></li>
<li><code>5 &rarr; &quot;101&quot;</code></li>
<li><code>7 &rarr; &quot;111&quot;</code></li>
<li><code>9 &rarr; &quot;1001&quot;</code></li>
</ul>
<p>All other values in <code>[0, 9]</code> have non-palindromic binary forms. Therefore, the count is 6.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Since <code>&quot;0&quot;</code> is a palindrome, the count is 1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>