670. Maximum Swap**
https://leetcode.com/problems/maximum-swap/
题目描述
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
Example 1:
Example 2:
Note:
The given number is in the range
C++ 实现 1
贪婪策略. 来自 LeetCode 的解答: https://leetcode.com/problems/maximum-swap/solution/
发现英文的表述更为精准, 这里引用一下:
Intuition
At each digit of the input number in order, if there is a larger digit that occurs later, we know that the best swap must occur with the digit we are currently considering.
Algorithm
We will compute the index of the last occurrence of digit (if it exists).
Afterwards, when scanning the number from left to right, if there is a larger digit in the future, we will swap it with the largest such digit; if there are multiple such digits, we will swap it with the one that occurs the latest.
概括来说, 依次访问 num
中的每个数字, 比如访问到 digits[i]
时, 如果后面还出现了更大的数字 k
, 那么用 k
来和 digits[i]
交换, 就能获取最大的结果. 为此, 从 [9, digits[i])
范围内搜索 k
.
下面代码中, 在 pos
保存每个数字的索引, 需要注意的是, 如果 num
存在相同的元素, 保存的是最远的元素的索引, 这样可以保证交换 和 时, 最后获得结果最大. 比如 1993
, 要用第二个 9
来和 1
交换.