-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
33 lines (27 loc) · 882 Bytes
/
Solution.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
//
// Greedy, Math(Maximum Swap)
// https://leetcode.com/problems/maximum-swap/
// Created by hyungwook on 2022/03/27.
//
import java.util.Arrays;
public class Solution {
public int maximumSwap(int num) {
char[] chars = String.valueOf(num).toCharArray();
char[] inc = String.valueOf(num).toCharArray();
Arrays.sort(inc);
for (int i = 0; i < chars.length; i++) {
if (chars[i] != inc[chars.length - 1 - i]) {
char tmp = chars[i];
chars[i] = inc[chars.length - 1 - i];
for (int j = chars.length - 1; j >= 0; j--) {
if (chars[j] == chars[i]) {
chars[j] = tmp;
break;
}
}
break;
}
}
return Integer.valueOf(String.valueOf(chars));
}
}