We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bb64aa7 commit 8845937Copy full SHA for 8845937
1432-max-difference-you-can-get-from-changing-an-integer/1432-max-difference-you-can-get-from-changing-an-integer.cpp
@@ -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
18
+ for (auto& i : b) {
19
+ if (i == x) i = '1';
20
21
+ } else {
22
+ x = -1;
23
24
+ if (i != b.front() && i != '0') {
25
26
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