mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
43 lines
1.1 KiB
HTML
43 lines
1.1 KiB
HTML
|
<p>给定一个正整数 <code>n</code> ,你可以做如下操作:</p>
|
|||
|
|
|||
|
<ol>
|
|||
|
<li>如果 <code>n</code><em> </em>是偶数,则用 <code>n / 2</code>替换 <code>n</code><em> </em>。</li>
|
|||
|
<li>如果 <code>n</code><em> </em>是奇数,则可以用 <code>n + 1</code>或<code>n - 1</code>替换 <code>n</code> 。</li>
|
|||
|
</ol>
|
|||
|
|
|||
|
<p>返回 <code>n</code><em> </em>变为 <code>1</code> 所需的 <em>最小替换次数</em> 。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例 1:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>n = 8
|
|||
|
<strong>输出:</strong>3
|
|||
|
<strong>解释:</strong>8 -> 4 -> 2 -> 1
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 2:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>n = 7
|
|||
|
<strong>输出:</strong>4
|
|||
|
<strong>解释:</strong>7 -> 8 -> 4 -> 2 -> 1
|
|||
|
或 7 -> 6 -> 3 -> 2 -> 1
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 3:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>n = 4
|
|||
|
<strong>输出:</strong>2
|
|||
|
</pre>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
|||
|
</ul>
|