1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-12 17:05:15 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/改变一个整数能得到的最大差值 [max-difference-you-can-get-from-changing-an-integer].html
2025-09-29 14:43:44 +08:00

65 lines
2.0 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>num</code>&nbsp;。你可以对它进行以下步骤共计&nbsp;<strong>两次</strong></p>
<ul>
<li>选择一个数字&nbsp;<code>x (0&nbsp;&lt;= x &lt;= 9)</code>.</li>
<li>选择另一个数字&nbsp;<code>y (0&nbsp;&lt;= y &lt;= 9)</code>&nbsp;。数字&nbsp;<code>y</code>&nbsp;可以等于&nbsp;<code>x</code>&nbsp;</li>
<li><code>num</code>&nbsp;中所有出现 <code>x</code>&nbsp;的数位都用 <code>y</code>&nbsp;替换。</li>
</ul>
<p>令两次对 <code>num</code>&nbsp;的操作得到的结果分别为&nbsp;<code>a</code>&nbsp;&nbsp;<code>b</code>&nbsp;</p>
<p>请你返回&nbsp;<code>a</code>&nbsp;<code>b</code>&nbsp;<strong>最大差值</strong></p>
<p>注意,<code>a</code>&nbsp;&nbsp;<code>b</code>&nbsp;<strong>必须不能</strong> 含有前导 0并且 <strong>不为</strong> 0。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>num = 555
<strong>输出:</strong>888
<strong>解释:</strong>第一次选择 x = 5 且 y = 9 ,并把得到的新数字保存在 a 中。
第二次选择 x = 5 且 y = 1 ,并把得到的新数字保存在 b 中。
现在,我们有 a = 999 和 b = 111 ,最大差值为 888
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>num = 9
<strong>输出:</strong>8
<strong>解释:</strong>第一次选择 x = 9 且 y = 9 ,并把得到的新数字保存在 a 中。
第二次选择 x = 9 且 y = 1 ,并把得到的新数字保存在 b 中。
现在,我们有 a = 9 和 b = 1 ,最大差值为 8
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>num = 123456
<strong>输出:</strong>820000
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>num = 10000
<strong>输出:</strong>80000
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>num = 9288
<strong>输出:</strong>8700
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 10^8</code></li>
</ul>