-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMaximumSwap.java
70 lines (62 loc) · 1.81 KB
/
MaximumSwap.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
66
67
68
69
70
package by.andd3dfx.numeric;
/**
* <pre>
* <a href="https://leetcode.com/problems/maximum-swap/">Task description</a>
*
* You are given an integer num. You can swap two digits at most once to get the maximum valued number.
* Return the maximum valued number you can get.
*
* Example 1:
*
* Input: num = 2736
* Output: 7236
* Explanation: Swap the number 2 and the number 7.
*
* Example 2:
*
* Input: num = 9973
* Output: 9973
* Explanation: No swap.
* </pre>
*
* @see <a href="https://youtu.be/-my6feBZ6RY">Video solution</a>
*/
public class MaximumSwap {
public static int solve(int num) {
var digits = numToDigits(num);
for (var i = 0; i < digits.length; i++) {
var maxIndex = i;
for (var j = i + 1; j < digits.length; j++) {
if (digits[maxIndex] < digits[j] || (digits[maxIndex] == digits[j] && maxIndex != i)) {
maxIndex = j;
}
}
if (maxIndex != i) {
swap(digits, i, maxIndex);
return digitsToNum(digits);
}
}
return num;
}
private static int[] numToDigits(int num) {
var str = String.valueOf(num);
int[] digits = new int[str.length()];
var pos = 0;
for (var digit : str.toCharArray()) {
digits[pos++] = digit - '0';
}
return digits;
}
private static int digitsToNum(int[] digits) {
int result = 0;
for (var pos = 0; pos < digits.length; pos++) {
result = 10 * result + digits[pos];
}
return result;
}
private static void swap(int[] items, int i, int j) {
var tmp = items[i];
items[i] = items[j];
items[j] = tmp;
}
}