Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
| [640. 求解方程](https://leetcode.cn/problems/solve-the-equation/) | [LeetCode 题解链接](https://leetcode.cn/problems/solve-the-equation/solution/by-ac_oier-fvee/) | 中等 | 🤩🤩🤩🤩 |
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
| [661. 图片平滑器](https://leetcode-cn.com/problems/image-smoother/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/image-smoother/solution/by-ac_oier-nn3v/) | 简单 | 🤩🤩🤩🤩 |
| [670. 最大交换](https://leetcode.cn/problems/maximum-swap/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-swap/solution/by-ac_oier-jxmh/) | 中等 | 🤩🤩🤩🤩 |
| [682. 棒球比赛](https://leetcode-cn.com/problems/baseball-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/baseball-game/solution/by-ac_oier-4mhz/) | 简单 | 🤩🤩🤩🤩 |
| [693. 交替位二进制数](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/solution/gong-si-shui-by-ac_oier-zuw7/) | 简单 | 🤩🤩🤩🤩🤩 |
| [709. 转换成小写字母](https://leetcode-cn.com/problems/to-lower-case/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/to-lower-case/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-czpo/) | 简单 | 🤩🤩🤩 |
Expand Down Expand Up @@ -137,7 +138,7 @@
| [1184. 公交站间的距离](https://leetcode.cn/problems/distance-between-bus-stops/) | [LeetCode 题解链接](https://leetcode.cn/problems/distance-between-bus-stops/solution/by-ac_oier-fow3/) | 简单 | 🤩🤩🤩🤩 |
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
| [1189. “气球” 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
| [1224. 最大相等频率](https://leetcode.cn/problems/maximum-equal-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-equal-frequency/solution/by-ac_oier-fviv/) | 困难 | |
| [1224. 最大相等频率](https://leetcode.cn/problems/maximum-equal-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-equal-frequency/solution/by-ac_oier-fviv/) | 困难 | 🤩🤩🤩🤩 |
| [1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/) | 简单 | 🤩🤩🤩 |
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
| [1282. 用户分组](https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/) | [LeetCode 题解链接](https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/solution/by-ac_oier-z1bg/) | 中等 | 🤩🤩🤩🤩🤩 |
Expand Down
109 changes: 109 additions & 0 deletions LeetCode/661-670/670. 最大交换(中等).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
### 题目描述

这是 LeetCode 上的 **[670. 最大交换](https://leetcode.cn/problems/maximum-swap/solution/by-ac_oier-jxmh/)** ,难度为 **中等**。

Tag : 「模拟」



给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。

示例 1 :
```
输入: 2736

输出: 7236

解释: 交换数字2和数字7。
```
示例 2 :
```
输入: 9973

输出: 9973

解释: 不需要交换。
```
注意:
* 给定数字的范围是 $[0, 10^8]$

---

### 模拟

根据题意,我们应当将大的数放置在高位,而当有数值相同的多个大数时,我们应当选择低位的数字。

因此,我们可以先将 `num` 的每一位处理出来存放到数组 `list` 中,随后预处理一个与 `list` 等长的数组 `idx`,带来代指 `num` 后缀中最大值对应的下标,即当 `idx[i] = j` 含义为在下标为 $[0, i]$ 位中 $num[j]$ 对应的数值最大。

同时由于我们需要遵循「当有数值相同的多个大数时,选择低位的数字」原则,我们应当出现采取严格大于才更新的方式来预处理 `idx`。

最后则是从高位往低位遍历,找到第一个替换的位置进行交换,并重新拼凑回答案。

Java 代码:
```Java
class Solution {
public int maximumSwap(int num) {
List<Integer> list = new ArrayList<>();
while (num != 0) {
list.add(num % 10); num /= 10;
}
int n = list.size(), ans = 0;
int[] idx = new int[n];
for (int i = 0, j = 0; i < n; i++) {
if (list.get(i) > list.get(j)) j = i;
idx[i] = j;
}
for (int i = n - 1; i >= 0; i--) {
if (list.get(idx[i]) != list.get(i)) {
int c = list.get(idx[i]);
list.set(idx[i], list.get(i));
list.set(i, c);
break;
}
}
for (int i = n - 1; i >= 0; i--) ans = ans * 10 + list.get(i);
return ans;
}
}
```
TypeScript 代码:
```TypeScript
function maximumSwap(num: number): number {
const list = new Array<number>()
while (num != 0) {
list.push(num % 10)
num = Math.floor(num / 10)
}
let n = list.length, ans = 0
const idx = new Array<number>()
for (let i = 0, j = 0; i < n; i++) {
if (list[i] > list[j]) j = i
idx.push(j)
}
for (let i = n - 1; i >= 0; i--) {
if (list[idx[i]] != list[i]) {
const c = list[idx[i]]
list[idx[i]] = list[i]
list[i] = c
break
}
}
for (let i = n - 1; i >= 0; i--) ans = ans * 10 + list[i];
return ans
};
```
* 时间复杂度:$O(\log{num})$
* 空间复杂度:$O(\log{num})$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.670` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。