Skip to content

Commit 4c4be55

Browse files
committed
feat: add solutions to lc problem: No.1432
No.1432.Max Difference You Can Get From Changing an Integer
1 parent 30eb399 commit 4c4be55

File tree

6 files changed

+163
-78
lines changed

6 files changed

+163
-78
lines changed

solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README.md

+58-28
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@
7373

7474
要想得到最大差值,那么我们应该拿到最大值与最小值,这样差值最大。
7575

76-
从高到低枚举 `nums` 每个位置上的数,如果数字不为 `9`,就将所有该数字替换为 `9`,得到最大整数 $a$。
76+
因此,我们先从高到低枚举 $nums$ 每个位置上的数,如果数字不为 `9`,就将所有该数字替换为 `9`,得到最大整数 $a$。
7777

78-
从高到低枚举 `nums` 每个位置上的数,首位不能为 `0`,因此如果首位不为 `1`,我们将其替换为 `1`;如果非首位,且数字不与首位相同,我们将其替换为 `0`得到最大整数 $b$。
78+
接下来,我们再从高到低枚举 `nums` 每个位置上的数,首位不能为 `0`,因此如果首位不为 `1`,我们将其替换为 `1`;如果非首位,且数字不与首位相同,我们将其替换为 `0`得到最大整数 $b$。
7979

8080
答案为差值 $a - b$。
8181

82-
时间复杂度 $O(log(num))$
82+
时间复杂度 $O(\log num)$,空间复杂度 $O(\log num)$。其中 $num$ 为给定整数
8383

8484
<!-- tabs:start -->
8585

@@ -92,17 +92,15 @@ class Solution:
9292
def maxDiff(self, num: int) -> int:
9393
a, b = str(num), str(num)
9494
for c in a:
95-
if c != '9':
96-
a = a.replace(c, '9')
95+
if c != "9":
96+
a = a.replace(c, "9")
9797
break
98-
for i, c in enumerate(b):
99-
if i == 0:
100-
if c != '1':
101-
b = b.replace(c, '1')
102-
break
103-
else:
104-
if c != '0' and c != b[0]:
105-
b = b.replace(c, '0')
98+
if b[0] != "1":
99+
b = b.replace(b[0], "1")
100+
else:
101+
for c in b[1:]:
102+
if c not in "01":
103+
b = b.replace(c, "0")
106104
break
107105
return int(a) - int(b)
108106
```
@@ -115,23 +113,19 @@ class Solution:
115113
class Solution {
116114
public int maxDiff(int num) {
117115
String a = String.valueOf(num);
118-
String b = String.valueOf(num);
119-
for (char c : a.toCharArray()) {
120-
if (c != '9') {
121-
a = a.replaceAll(String.valueOf(c), "9");
116+
String b = a;
117+
for (int i = 0; i < a.length(); ++i) {
118+
if (a.charAt(i) != '9') {
119+
a = a.replace(a.charAt(i), '9');
122120
break;
123121
}
124122
}
125-
for (int i = 0; i < b.length(); ++i) {
126-
char c = b.charAt(i);
127-
if (i == 0) {
128-
if (c != '1') {
129-
b = b.replaceAll(String.valueOf(c), "1");
130-
break;
131-
}
132-
} else {
133-
if (c != '0' && c != b.charAt(0)) {
134-
b = b.replaceAll(String.valueOf(c), "0");
123+
if (b.charAt(0) != '1') {
124+
b = b.replace(b.charAt(0), '1');
125+
} else {
126+
for (int i = 1; i < b.length(); ++i) {
127+
if (b.charAt(i) != '0' && b.charAt(i) != '1') {
128+
b = b.replace(b.charAt(i), '0');
135129
break;
136130
}
137131
}
@@ -141,6 +135,42 @@ class Solution {
141135
}
142136
```
143137

138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
int maxDiff(int num) {
144+
auto replace = [](string& s, char a, char b) {
145+
for (auto& c : s) {
146+
if (c == a) {
147+
c = b;
148+
}
149+
}
150+
};
151+
string a = to_string(num);
152+
string b = a;
153+
for (int i = 0; i < a.size(); ++i) {
154+
if (a[i] != '9') {
155+
replace(a, a[i], '9');
156+
break;
157+
}
158+
}
159+
if (b[0] != '1') {
160+
replace(b, b[0], '1');
161+
} else {
162+
for (int i = 1; i < b.size(); ++i) {
163+
if (b[i] != '0' && b[i] != '1') {
164+
replace(b, b[i], '0');
165+
break;
166+
}
167+
}
168+
}
169+
return stoi(a) - stoi(b);
170+
}
171+
};
172+
```
173+
144174
### **Go**
145175
146176
```go
@@ -157,7 +187,7 @@ func maxDiff(num int) int {
157187
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[0]), "1"))
158188
} else {
159189
for i := 1; i < len(s); i++ {
160-
if s[i] != '0' && s[i] != s[0] {
190+
if s[i] != '0' && s[i] != '1' {
161191
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[i]), "0"))
162192
break
163193
}

solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README_EN.md

+55-25
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,15 @@ class Solution:
5656
def maxDiff(self, num: int) -> int:
5757
a, b = str(num), str(num)
5858
for c in a:
59-
if c != '9':
60-
a = a.replace(c, '9')
59+
if c != "9":
60+
a = a.replace(c, "9")
6161
break
62-
for i, c in enumerate(b):
63-
if i == 0:
64-
if c != '1':
65-
b = b.replace(c, '1')
66-
break
67-
else:
68-
if c != '0' and c != b[0]:
69-
b = b.replace(c, '0')
62+
if b[0] != "1":
63+
b = b.replace(b[0], "1")
64+
else:
65+
for c in b[1:]:
66+
if c not in "01":
67+
b = b.replace(c, "0")
7068
break
7169
return int(a) - int(b)
7270
```
@@ -77,23 +75,19 @@ class Solution:
7775
class Solution {
7876
public int maxDiff(int num) {
7977
String a = String.valueOf(num);
80-
String b = String.valueOf(num);
81-
for (char c : a.toCharArray()) {
82-
if (c != '9') {
83-
a = a.replaceAll(String.valueOf(c), "9");
78+
String b = a;
79+
for (int i = 0; i < a.length(); ++i) {
80+
if (a.charAt(i) != '9') {
81+
a = a.replace(a.charAt(i), '9');
8482
break;
8583
}
8684
}
87-
for (int i = 0; i < b.length(); ++i) {
88-
char c = b.charAt(i);
89-
if (i == 0) {
90-
if (c != '1') {
91-
b = b.replaceAll(String.valueOf(c), "1");
92-
break;
93-
}
94-
} else {
95-
if (c != '0' && c != b.charAt(0)) {
96-
b = b.replaceAll(String.valueOf(c), "0");
85+
if (b.charAt(0) != '1') {
86+
b = b.replace(b.charAt(0), '1');
87+
} else {
88+
for (int i = 1; i < b.length(); ++i) {
89+
if (b.charAt(i) != '0' && b.charAt(i) != '1') {
90+
b = b.replace(b.charAt(i), '0');
9791
break;
9892
}
9993
}
@@ -103,6 +97,42 @@ class Solution {
10397
}
10498
```
10599

100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int maxDiff(int num) {
106+
auto replace = [](string& s, char a, char b) {
107+
for (auto& c : s) {
108+
if (c == a) {
109+
c = b;
110+
}
111+
}
112+
};
113+
string a = to_string(num);
114+
string b = a;
115+
for (int i = 0; i < a.size(); ++i) {
116+
if (a[i] != '9') {
117+
replace(a, a[i], '9');
118+
break;
119+
}
120+
}
121+
if (b[0] != '1') {
122+
replace(b, b[0], '1');
123+
} else {
124+
for (int i = 1; i < b.size(); ++i) {
125+
if (b[i] != '0' && b[i] != '1') {
126+
replace(b, b[i], '0');
127+
break;
128+
}
129+
}
130+
}
131+
return stoi(a) - stoi(b);
132+
}
133+
};
134+
```
135+
106136
### **Go**
107137
108138
```go
@@ -119,7 +149,7 @@ func maxDiff(num int) int {
119149
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[0]), "1"))
120150
} else {
121151
for i := 1; i < len(s); i++ {
122-
if s[i] != '0' && s[i] != s[0] {
152+
if s[i] != '0' && s[i] != '1' {
123153
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[i]), "0"))
124154
break
125155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int maxDiff(int num) {
4+
auto replace = [](string& s, char a, char b) {
5+
for (auto& c : s) {
6+
if (c == a) {
7+
c = b;
8+
}
9+
}
10+
};
11+
string a = to_string(num);
12+
string b = a;
13+
for (int i = 0; i < a.size(); ++i) {
14+
if (a[i] != '9') {
15+
replace(a, a[i], '9');
16+
break;
17+
}
18+
}
19+
if (b[0] != '1') {
20+
replace(b, b[0], '1');
21+
} else {
22+
for (int i = 1; i < b.size(); ++i) {
23+
if (b[i] != '0' && b[i] != '1') {
24+
replace(b, b[i], '0');
25+
break;
26+
}
27+
}
28+
}
29+
return stoi(a) - stoi(b);
30+
}
31+
};

solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func maxDiff(num int) int {
1111
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[0]), "1"))
1212
} else {
1313
for i := 1; i < len(s); i++ {
14-
if s[i] != '0' && s[i] != s[0] {
14+
if s[i] != '0' && s[i] != '1' {
1515
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[i]), "0"))
1616
break
1717
}

solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public int maxDiff(int num) {
33
String a = String.valueOf(num);
4-
String b = String.valueOf(num);
5-
for (char c : a.toCharArray()) {
6-
if (c != '9') {
7-
a = a.replaceAll(String.valueOf(c), "9");
4+
String b = a;
5+
for (int i = 0; i < a.length(); ++i) {
6+
if (a.charAt(i) != '9') {
7+
a = a.replace(a.charAt(i), '9');
88
break;
99
}
1010
}
11-
for (int i = 0; i < b.length(); ++i) {
12-
char c = b.charAt(i);
13-
if (i == 0) {
14-
if (c != '1') {
15-
b = b.replaceAll(String.valueOf(c), "1");
16-
break;
17-
}
18-
} else {
19-
if (c != '0' && c != b.charAt(0)) {
20-
b = b.replaceAll(String.valueOf(c), "0");
11+
if (b.charAt(0) != '1') {
12+
b = b.replace(b.charAt(0), '1');
13+
} else {
14+
for (int i = 1; i < b.length(); ++i) {
15+
if (b.charAt(i) != '0' && b.charAt(i) != '1') {
16+
b = b.replace(b.charAt(i), '0');
2117
break;
2218
}
2319
}

solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ class Solution:
22
def maxDiff(self, num: int) -> int:
33
a, b = str(num), str(num)
44
for c in a:
5-
if c != '9':
6-
a = a.replace(c, '9')
5+
if c != "9":
6+
a = a.replace(c, "9")
77
break
8-
for i, c in enumerate(b):
9-
if i == 0:
10-
if c != '1':
11-
b = b.replace(c, '1')
12-
break
13-
else:
14-
if c != '0' and c != b[0]:
15-
b = b.replace(c, '0')
8+
if b[0] != "1":
9+
b = b.replace(b[0], "1")
10+
else:
11+
for c in b[1:]:
12+
if c not in "01":
13+
b = b.replace(c, "0")
1614
break
1715
return int(a) - int(b)

0 commit comments

Comments
 (0)