最大交换
难度:
标签:
题目描述
代码结果
运行时间: 15 ms, 内存: 16.0 MB
/*
* Problem: Given a non-negative integer, you can swap at most once two digits of the number to get the maximum possible value.
* Solution: Convert the number to a character array, then find the highest possible number by swapping two digits.
* - Use Java Streams to manage the manipulation of the number as an array of digits.
* - Find the maximum digit that can be swapped to increase the number.
*/
import java.util.stream.IntStream;
public class Solution {
public int maximumSwap(int num) {
char[] digits = Integer.toString(num).toCharArray();
int n = digits.length;
int[] maxIndex = IntStream.range(0, n).toArray();
// Record the position of the max digit to the right of each digit
IntStream.range(0, n - 1).forEach(i -> {
maxIndex[i] = IntStream.range(i + 1, n).boxed()
.max((j1, j2) -> digits[j1] - digits[j2])
.orElse(i);
});
// Find the first position where a larger digit can be swapped
int pos = IntStream.range(0, n)
.filter(i -> digits[i] < digits[maxIndex[i]])
.findFirst()
.orElse(-1);
if (pos != -1) {
// Swap
char temp = digits[pos];
digits[pos] = digits[maxIndex[pos]];
digits[maxIndex[pos]] = temp;
}
return Integer.parseInt(new String(digits));
}
}
解释
方法:
这个题解采用了枚举法的思路。对于一个给定的数字,我们可以枚举所有可能的交换位置(即任意两个数字交换),然后比较交换后的数字大小,取其中最大的一个作为最终答案。具体实现时,先将数字转化为字符串,然后使用两重循环枚举所有交换的可能性,每次交换后都更新答案为当前最大值。
时间复杂度:
O(n^2)
空间复杂度:
O(n)
代码细节讲解
🦆
题解中提到枚举所有可能的交换位置,但没有说明为什么不考虑多次交换,能否解释一下?
▷🦆
在两重循环中,内循环的起始位置为什么是从 i 开始而不是从 0 或 i+1 开始?这是否会影响到算法的效率或结果?
▷🦆
题解中使用了`max`函数来比较和更新最大值,这样的操作是否会影响到算法的整体效率?
▷🦆
在进行数字交换后,为什么要将数字恢复到原始状态而不是继续使用已交换的版本进行后续操作?
▷相关问题
拼接最大数
给你两个整数数组 nums1
和 nums2
,它们的长度分别为 m
和 n
。数组 nums1
和 nums2
分别代表两个数各位上的数字。同时你也会得到一个整数 k
。
请你利用这两个数组中的数字中创建一个长度为 k <= m + n
的最大数,在这个必须保留来自同一数组的数字的相对顺序。
返回代表答案的长度为 k
的数组。
示例 1:
输入:nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5 输出:[9,8,6,5,3]
示例 2:
输入:nums1 = [6,7], nums2 = [6,0,4], k = 5 输出:[6,7,6,0,4]
示例 3:
输入:nums1 = [3,9], nums2 = [8,9], k = 3 输出:[9,8,9]
提示:
m == nums1.length
n == nums2.length
1 <= m, n <= 500
0 <= nums1[i], nums2[i] <= 9
1 <= k <= m + n