Skip to content

Commit fda3692

Browse files
authored
Create 2566. Maximum Difference by Remapping a Digit (#816)
2 parents 59c49a0 + 36590ce commit fda3692

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int minMaxDifference(int num) {
4+
string s = to_string(num);
5+
6+
string s_max = s;
7+
for (char c : s) {
8+
if (c != '9') {
9+
for (char &ch : s_max) {
10+
if (ch == c) ch = '9';
11+
}
12+
break;
13+
}
14+
}
15+
16+
string s_min = s;
17+
for (char c : s) {
18+
if (c != '0') {
19+
for (char &ch : s_min) {
20+
if (ch == c) ch = '0';
21+
}
22+
break;
23+
}
24+
}
25+
26+
return stoi(s_max) - stoi(s_min);
27+
}
28+
};

0 commit comments

Comments
 (0)