Skip to content

Commit 8845937

Browse files
committed
Time: 0 ms (100%), Space: 8.2 MB (43.13%) - LeetHub
1 parent bb64aa7 commit 8845937

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int maxDiff(int num) {
4+
string a = to_string(num);
5+
string b = a;
6+
char x = '9';
7+
8+
for (auto& i : a) {
9+
if (i != '9') {
10+
x = i;
11+
break;
12+
}
13+
}
14+
for (auto& i : a) if (i == x) i = '9';
15+
x = b.front();
16+
if (b.front() != '1') {
17+
x = b.front();
18+
for (auto& i : b) {
19+
if (i == x) i = '1';
20+
}
21+
} else {
22+
x = -1;
23+
for (auto& i : b) {
24+
if (i != b.front() && i != '0') {
25+
x = i;
26+
break;
27+
}
28+
}
29+
for (int i = 1; i < b.size(); i++) {
30+
if (b[i] == x) b[i] = '0';
31+
}
32+
}
33+
return stoi(a) - stoi(b);
34+
}
35+
};

0 commit comments

Comments
 (0)