mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 670.cpp
This commit is contained in:
parent
ed25ac1abe
commit
548cb4d474
30
LeetCode-CN/670.cpp
Normal file
30
LeetCode-CN/670.cpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user