Skip to content

Commit 12ef2bb

Browse files
committed
Time: 0 ms (100%), Space: 7.6 MB (70.34%) - LeetHub
1 parent 95bd7b6 commit 12ef2bb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maximumSwap(int num) {
4+
string x;
5+
x = to_string(num);
6+
int n = x.size();
7+
for (int i = 0; i < n; i++) {
8+
int mx = i;
9+
for (int j = i + 1; j < n; j++) {
10+
if (x[j] >= x[mx]) {
11+
mx = j;
12+
}
13+
}
14+
if (x[mx] != x[i]) {
15+
swap(x[i], x[mx]);
16+
break;
17+
}
18+
}
19+
return stoi(x);
20+
}
21+
};

0 commit comments

Comments
 (0)