Create 670.cpp

This commit is contained in:
Kirigaya Kazuto 2022-09-13 08:55:45 +08:00 committed by GitHub
parent ed25ac1abe
commit 548cb4d474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

30
LeetCode-CN/670.cpp Normal file
View File

@ -0,0 +1,30 @@
class Solution {
public:
int maximumSwap(int num) {
char buff[32];
snprintf(buff, 32, "%d", num);
int len = strlen(buff);
for (int i = 0; i < len - 1; i++)
{
int maxIndex = i + 1;
char maxChar = buff[i + 1];
for (int j = i + 2; j < len; j++)
{
if (buff[j] >= maxChar) {
maxChar = buff[j];
maxIndex = j;
}
}
if (buff[i] < maxChar) {
swap(buff[i], buff[maxIndex]);
break;
}
}
int result;
sscanf(buff, "%d", &result);
return result;
}
};