1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/整数替换 [integer-replacement].html

43 lines
1.1 KiB
HTML
Raw Normal View History

2022-03-27 20:56:26 +08:00
<p>给定一个正整数&nbsp;<code>n</code> ,你可以做如下操作:</p>
<ol>
<li>如果&nbsp;<code>n</code><em>&nbsp;</em>是偶数,则用&nbsp;<code>n / 2</code>替换&nbsp;<code>n</code><em> </em></li>
<li>如果&nbsp;<code>n</code><em>&nbsp;</em>是奇数,则可以用&nbsp;<code>n + 1</code><code>n - 1</code>替换&nbsp;<code>n</code></li>
</ol>
<p>返回 <code>n</code><em>&nbsp;</em>变为 <code>1</code> 所需的 <em>最小替换次数</em></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 8
<strong>输出:</strong>3
<strong>解释:</strong>8 -&gt; 4 -&gt; 2 -&gt; 1
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 7
<strong>输出:</strong>4
<strong>解释:</strong>7 -&gt; 8 -&gt; 4 -&gt; 2 -&gt; 1
或 7 -&gt; 6 -&gt; 3 -&gt; 2 -&gt; 1
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>n = 4
<strong>输出:</strong>2
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
</ul>