-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0670.最大交换.java
65 lines (61 loc) · 1.32 KB
/
0670.最大交换.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=670 lang=java
*
* [670] 最大交换
*
* https://leetcode.cn/problems/maximum-swap/description/
*
* algorithms
* Medium (48.01%)
* Likes: 450
* Dislikes: 0
* Total Accepted: 81.4K
* Total Submissions: 167K
* Testcase Example: '2736'
*
* 给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。
*
* 示例 1 :
*
*
* 输入: 2736
* 输出: 7236
* 解释: 交换数字2和数字7。
*
*
* 示例 2 :
*
*
* 输入: 9973
* 输出: 9973
* 解释: 不需要交换。
*
*
* 注意:
*
*
* 给定数字的范围是 [0, 10^8]
*
*
*/
// @lc code=start
class Solution {
public int maximumSwap(int num) {
int[] digits = new int[10];
char[] chars = String.valueOf(num).toCharArray();
for (int i = 0; i < chars.length; i++)
digits[chars[i] - '0'] = i;
for (int i = 0; i < chars.length; i++) {
for (int j = 9; j > chars[i] - '0'; j--) {
if (digits[j] > i) {
char temp = chars[i];
chars[i] = chars[digits[j]];
chars[digits[j]] = temp;
return Integer.parseInt(new String(chars));
}
}
}
return num;
}
}
// @lc code=end