Skip to content

Commit 8474ca1

Browse files
committed
✨feat: add 670
1 parent b549089 commit 8474ca1

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

Index/模拟.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| [640. 求解方程](https://leetcode.cn/problems/solve-the-equation/) | [LeetCode 题解链接](https://leetcode.cn/problems/solve-the-equation/solution/by-ac_oier-fvee/) | 中等 | 🤩🤩🤩🤩 |
8888
| [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/) | 简单 | 🤩🤩🤩 |
8989
| [661. 图片平滑器](https://leetcode-cn.com/problems/image-smoother/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/image-smoother/solution/by-ac_oier-nn3v/) | 简单 | 🤩🤩🤩🤩 |
90+
| [670. 最大交换](https://leetcode.cn/problems/maximum-swap/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-swap/solution/by-ac_oier-jxmh/) | 中等 | 🤩🤩🤩🤩 |
9091
| [682. 棒球比赛](https://leetcode-cn.com/problems/baseball-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/baseball-game/solution/by-ac_oier-4mhz/) | 简单 | 🤩🤩🤩🤩 |
9192
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
9293
| [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/) | 简单 | 🤩🤩🤩 |
@@ -137,7 +138,7 @@
137138
| [1184. 公交站间的距离](https://leetcode.cn/problems/distance-between-bus-stops/) | [LeetCode 题解链接](https://leetcode.cn/problems/distance-between-bus-stops/solution/by-ac_oier-fow3/) | 简单 | 🤩🤩🤩🤩 |
138139
| [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/) | 简单 | 🤩🤩🤩🤩 |
139140
| [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/) | 简单 | 🤩🤩🤩🤩 |
140-
| [1224. 最大相等频率](https://leetcode.cn/problems/maximum-equal-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-equal-frequency/solution/by-ac_oier-fviv/) | 困难 | |
141+
| [1224. 最大相等频率](https://leetcode.cn/problems/maximum-equal-frequency/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-equal-frequency/solution/by-ac_oier-fviv/) | 困难 | 🤩🤩🤩🤩 |
141142
| [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/) | 简单 | 🤩🤩🤩 |
142143
| [1260. 二维网格迁移](https://leetcode.cn/problems/shift-2d-grid/) | [LeetCode 题解链接](https://leetcode.cn/problems/shift-2d-grid/solution/by-ac_oier-1blt/) | 简单 | 🤩🤩🤩🤩 |
143144
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[670. 最大交换](https://leetcode.cn/problems/maximum-swap/solution/by-ac_oier-jxmh/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。
10+
11+
示例 1 :
12+
```
13+
输入: 2736
14+
15+
输出: 7236
16+
17+
解释: 交换数字2和数字7。
18+
```
19+
示例 2 :
20+
```
21+
输入: 9973
22+
23+
输出: 9973
24+
25+
解释: 不需要交换。
26+
```
27+
注意:
28+
* 给定数字的范围是 $[0, 10^8]$
29+
30+
---
31+
32+
### 模拟
33+
34+
根据题意,我们应当将大的数放置在高位,而当有数值相同的多个大数时,我们应当选择低位的数字。
35+
36+
因此,我们可以先将 `num` 的每一位处理出来存放到数组 `list` 中,随后预处理一个与 `list` 等长的数组 `idx`,带来代指 `num` 后缀中最大值对应的下标,即当 `idx[i] = j` 含义为在下标为 $[0, i]$ 位中 $num[j]$ 对应的数值最大。
37+
38+
同时由于我们需要遵循「当有数值相同的多个大数时,选择低位的数字」原则,我们应当出现采取严格大于才更新的方式来预处理 `idx`
39+
40+
最后则是从高位往低位遍历,找到第一个替换的位置进行交换,并重新拼凑回答案。
41+
42+
Java 代码:
43+
```Java
44+
class Solution {
45+
public int maximumSwap(int num) {
46+
List<Integer> list = new ArrayList<>();
47+
while (num != 0) {
48+
list.add(num % 10); num /= 10;
49+
}
50+
int n = list.size(), ans = 0;
51+
int[] idx = new int[n];
52+
for (int i = 0, j = 0; i < n; i++) {
53+
if (list.get(i) > list.get(j)) j = i;
54+
idx[i] = j;
55+
}
56+
for (int i = n - 1; i >= 0; i--) {
57+
if (list.get(idx[i]) != list.get(i)) {
58+
int c = list.get(idx[i]);
59+
list.set(idx[i], list.get(i));
60+
list.set(i, c);
61+
break;
62+
}
63+
}
64+
for (int i = n - 1; i >= 0; i--) ans = ans * 10 + list.get(i);
65+
return ans;
66+
}
67+
}
68+
```
69+
TypeScript 代码:
70+
```TypeScript
71+
function maximumSwap(num: number): number {
72+
const list = new Array<number>()
73+
while (num != 0) {
74+
list.push(num % 10)
75+
num = Math.floor(num / 10)
76+
}
77+
let n = list.length, ans = 0
78+
const idx = new Array<number>()
79+
for (let i = 0, j = 0; i < n; i++) {
80+
if (list[i] > list[j]) j = i
81+
idx.push(j)
82+
}
83+
for (let i = n - 1; i >= 0; i--) {
84+
if (list[idx[i]] != list[i]) {
85+
const c = list[idx[i]]
86+
list[idx[i]] = list[i]
87+
list[i] = c
88+
break
89+
}
90+
}
91+
for (let i = n - 1; i >= 0; i--) ans = ans * 10 + list[i];
92+
return ans
93+
};
94+
```
95+
* 时间复杂度:$O(\log{num})$
96+
* 空间复杂度:$O(\log{num})$
97+
98+
---
99+
100+
### 最后
101+
102+
这是我们「刷穿 LeetCode」系列文章的第 `No.670` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
103+
104+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
105+
106+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
107+
108+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
109+

0 commit comments

Comments
 (0)