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

43 lines
1.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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>