73
73
74
74
要想得到最大差值,那么我们应该拿到最大值与最小值,这样差值最大。
75
75
76
- 从高到低枚举 ` nums ` 每个位置上的数,如果数字不为 ` 9 ` ,就将所有该数字替换为 ` 9 ` ,得到最大整数 $a$。
76
+ 因此,我们先从高到低枚举 $ nums$ 每个位置上的数,如果数字不为 ` 9 ` ,就将所有该数字替换为 ` 9 ` ,得到最大整数 $a$。
77
77
78
- 从高到低枚举 ` nums ` 每个位置上的数,首位不能为 ` 0 ` ,因此如果首位不为 ` 1 ` ,我们将其替换为 ` 1 ` ;如果非首位,且数字不与首位相同,我们将其替换为 ` 0 ` 。 得到最大整数 $b$。
78
+ 接下来,我们再从高到低枚举 ` nums ` 每个位置上的数,首位不能为 ` 0 ` ,因此如果首位不为 ` 1 ` ,我们将其替换为 ` 1 ` ;如果非首位,且数字不与首位相同,我们将其替换为 ` 0 ` , 得到最大整数 $b$。
79
79
80
80
答案为差值 $a - b$。
81
81
82
- 时间复杂度 $O(log( num))$ 。
82
+ 时间复杂度 $O(\ log num)$,空间复杂度 $O(\log num)$。其中 $num$ 为给定整数 。
83
83
84
84
<!-- tabs:start -->
85
85
@@ -92,17 +92,15 @@ class Solution:
92
92
def maxDiff (self , num : int ) -> int :
93
93
a, b = str (num), str (num)
94
94
for c in a:
95
- if c != ' 9 ' :
96
- a = a.replace(c, ' 9 ' )
95
+ if c != " 9 " :
96
+ a = a.replace(c, " 9 " )
97
97
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" )
106
104
break
107
105
return int (a) - int (b)
108
106
```
@@ -115,23 +113,19 @@ class Solution:
115
113
class Solution {
116
114
public int maxDiff (int num ) {
117
115
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 ' );
122
120
break ;
123
121
}
124
122
}
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' );
135
129
break ;
136
130
}
137
131
}
@@ -141,6 +135,42 @@ class Solution {
141
135
}
142
136
```
143
137
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
+
144
174
### **Go**
145
175
146
176
```go
@@ -157,7 +187,7 @@ func maxDiff(num int) int {
157
187
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[0]), "1"))
158
188
} else {
159
189
for i := 1; i < len(s); i++ {
160
- if s[i] != ' 0' && s[i] != s[ 0 ] {
190
+ if s[i] != '0' && s[i] != '1' {
161
191
b, _ = strconv.Atoi(strings.ReplaceAll(s, string(s[i]), "0"))
162
192
break
163
193
}
0 commit comments