leetcode 670 最大交换
作者:互联网
简介
code
public class Solution {
public int maximumSwap(int num) {
String s = String.valueOf(num);
int len = s.length();
char[] charArray = s.toCharArray();
int max = num;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
swap(charArray, i, j);
max = Math.max(max, Integer.parseInt(new String(charArray))); // 先编码 再解码
swap(charArray, i, j);
}
}
return max;
}
private void swap(char[] charArray, int index1, int index2) {
char temp = charArray[index1];
charArray[index1] = charArray[index2];
charArray[index2] = temp;
}
}
标签:int,670,交换,char,charArray,num,swap,max,leetcode 来源: https://www.cnblogs.com/eat-too-much/p/14882517.html