Description
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:
1 | Input: 2736 |
Example 2:
1 | Input: 9973 |
Note:
- The given number is in the range [0, 108]
解法
思路:先找到每个位置对应的在他右边的最大的数,然后从高位起扫描数组,找到第一个存在右边比他大的数的位置,然后从数组尾部开始扫描,找到这个比他大的数,然后交换即可
具体代码如下:
1 | class Solution { |