-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path1432.cpp
More file actions
26 lines (25 loc) · 692 Bytes
/
1432.cpp
File metadata and controls
26 lines (25 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int maxDiff(int num) {
string a = to_string(num), b = a;
for (char digit : a) {
if (digit != '9') {
replace(a.begin(), a.end(), digit, '9');
break;
}
}
if (b[0] != '1') {
char c = b[0];
replace(b.begin(), b.end(), c, '1');
} else {
for (int i = 1; i < b.size(); i++) {
if (b[i] != '0' && b[i] != '1') {
char c = b[i];
replace(b.begin(), b.end(), c, '0');
break;
}
}
}
return stoi(a) - stoi(b);
}
};