670 Maximum Swap¶
Problem¶
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
Example 1:
Example 2: Note: The given number is in the range [0, 108]Solutions¶
class Solution {
public int maximumSwap(int num) {
StringBuilder sb = new StringBuilder();
while (num > 0) {
sb.insert(0, num % 10);
num = num / 10;
}
char[] chars = sb.toString().toCharArray();
char[] maxs = new char[chars.length];
int[] indexes = new int[chars.length];
for (int i = chars.length - 1; i >= 0; i --) {
if (i == chars.length - 1) {
maxs[i] = chars[i];
indexes[i] = i;
}
else {
if (chars[i] > maxs[i + 1]) {
maxs[i] = chars[i];
indexes[i] = i;
}
else {
maxs[i] = maxs[i + 1];
indexes[i] = indexes[i + 1];
}
}
}
for (int i = 0; i < chars.length; i ++) {
if (maxs[i] > chars[i]) {
//swap i and indexes[i];
char c = chars[i];
chars[i] = chars[indexes[i]];
chars[indexes[i]] = c;
break;
}
}
int res = 0;
for (int i = 0; i < chars.length; i ++) {
res = res * 10 + (chars[i] - '0');
}
return res;
}
}