直接对着题目翻译
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num string字符串
* @return string字符串
*/
static bool cmp(const int &a, const int &b) {
return a > b;
}
string maximumSwap(string num) {
// write code here
string str1;
int a[1000500];
int len = num.size();
for (int i = 0; i < len; i++) {
a[i] = num[i] - '0';
}
sort(a, a + len, cmp);
for (int i = 0; i < len; i++) {
str1 += a[i] + '0';
}
int mark = 0;
for (int i = 0; i < len; i++) {
if (num[i] != str1[i]) {
mark = i;
break;
}
}
int mark1 = 0;
for (int i = 0; i < len; i++) {
if (str1[mark] == num[i]) {
mark1 = i;
}
}
char temp = num[mark];
num[mark] = num[mark1];
num[mark1] = temp;
return num;
}
};