mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-13 01:15:14 +08:00
update
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<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 <= k <= 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>"0"</code>.</p>
|
||||
|
||||
<p> </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 → "0"</code></li>
|
||||
<li><code>1 → "1"</code></li>
|
||||
<li><code>3 → "11"</code></li>
|
||||
<li><code>5 → "101"</code></li>
|
||||
<li><code>7 → "111"</code></li>
|
||||
<li><code>9 → "1001"</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>"0"</code> is a palindrome, the count is 1.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>15</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user