Skip to content

Commit 0c33e0b

Browse files
authored
feat: add solutions to lc problem: No.2566 (#4492)
No.2566.Maximum Difference by Remapping a Digit
1 parent 370d053 commit 0c33e0b

File tree

7 files changed

+155
-241
lines changed

7 files changed

+155
-241
lines changed

solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README.md

Lines changed: 52 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tags:
7979

8080
最后返回最大值和最小值的差即可。
8181

82-
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数字 $num$ 的大小
82+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数字 $\textit{num}$ 的值
8383

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

@@ -177,14 +177,15 @@ func minMaxDifference(num int) int {
177177

178178
```ts
179179
function minMaxDifference(num: number): number {
180-
const s = num + '';
181-
const min = Number(s.replace(new RegExp(s[0], 'g'), '0'));
180+
const s = num.toString();
181+
const mi = +s.replaceAll(s[0], '0');
182182
for (const c of s) {
183183
if (c !== '9') {
184-
return Number(s.replace(new RegExp(c, 'g'), '9')) - min;
184+
const mx = +s.replaceAll(c, '9');
185+
return mx - mi;
185186
}
186187
}
187-
return num - min;
188+
return num - mi;
188189
}
189190
```
190191

@@ -194,103 +195,69 @@ function minMaxDifference(num: number): number {
194195
impl Solution {
195196
pub fn min_max_difference(num: i32) -> i32 {
196197
let s = num.to_string();
197-
let min = s
198-
.replace(char::from(s.as_bytes()[0]), "0")
199-
.parse::<i32>()
200-
.unwrap();
201-
for &c in s.as_bytes() {
202-
if c != b'9' {
203-
return s.replace(c, "9").parse().unwrap() - min;
198+
let mi = s.replace(s.chars().next().unwrap(), "0").parse::<i32>().unwrap();
199+
for c in s.chars() {
200+
if c != '9' {
201+
let mx = s.replace(c, "9").parse::<i32>().unwrap();
202+
return mx - mi;
204203
}
205204
}
206-
num - min
205+
num - mi
207206
}
208207
}
209208
```
210209

211-
#### C
210+
#### JavaScript
212211

213-
```c
214-
int getLen(int num) {
215-
int res = 0;
216-
while (num) {
217-
num /= 10;
218-
res++;
219-
}
220-
return res;
221-
}
222-
223-
int minMaxDifference(int num) {
224-
int n = getLen(num);
225-
int* nums = malloc(sizeof(int) * n);
226-
int t = num;
227-
for (int i = n - 1; i >= 0; i--) {
228-
nums[i] = t % 10;
229-
t /= 10;
230-
}
231-
int min = 0;
232-
for (int i = 0; i < n; i++) {
233-
min *= 10;
234-
if (nums[i] != nums[0]) {
235-
min += nums[i];
236-
}
237-
}
238-
int max = 0;
239-
int target = 10;
240-
for (int i = 0; i < n; i++) {
241-
max *= 10;
242-
if (target == 10 && nums[i] != 9) {
243-
target = nums[i];
212+
```js
213+
/**
214+
* @param {number} num
215+
* @return {number}
216+
*/
217+
var minMaxDifference = function (num) {
218+
const s = num.toString();
219+
const mi = +s.replaceAll(s[0], '0');
220+
for (const c of s) {
221+
if (c !== '9') {
222+
const mx = +s.replaceAll(c, '9');
223+
return mx - mi;
244224
}
245-
max += nums[i] == target ? 9 : nums[i];
246225
}
247-
free(nums);
248-
return max - min;
249-
}
226+
return num - mi;
227+
};
250228
```
251229

252-
<!-- tabs:end -->
253-
254-
<!-- solution:end -->
255-
256-
<!-- solution:start -->
257-
258-
### 方法二
259-
260-
<!-- tabs:start -->
261-
262-
#### Rust
230+
#### C
263231

264-
```rust
265-
impl Solution {
266-
pub fn min_max_difference(num: i32) -> i32 {
267-
let mut s = num.to_string().into_bytes();
268-
let first = s[0];
269-
for i in 0..s.len() {
270-
if s[i] == first {
271-
s[i] = b'0';
272-
}
232+
```c
233+
int minMaxDifference(int num) {
234+
char s[12];
235+
sprintf(s, "%d", num);
236+
237+
int mi;
238+
{
239+
char tmp[12];
240+
char t = s[0];
241+
for (int i = 0; s[i]; i++) {
242+
tmp[i] = (s[i] == t) ? '0' : s[i];
273243
}
274-
let mi = String::from_utf8_lossy(&s).parse::<i32>().unwrap();
275-
276-
let mut t = num.to_string().into_bytes();
277-
for i in 0..t.len() {
278-
if t[i] != b'9' {
279-
let second = t[i];
280-
281-
for j in 0..t.len() {
282-
if t[j] == second {
283-
t[j] = b'9';
284-
}
285-
}
244+
tmp[strlen(s)] = '\0';
245+
mi = atoi(tmp);
246+
}
286247

287-
let mx = String::from_utf8_lossy(&t).parse::<i32>().unwrap();
288-
return mx - mi;
248+
for (int i = 0; s[i]; i++) {
249+
char c = s[i];
250+
if (c != '9') {
251+
char tmp[12];
252+
for (int j = 0; s[j]; j++) {
253+
tmp[j] = (s[j] == c) ? '9' : s[j];
289254
}
255+
tmp[strlen(s)] = '\0';
256+
return atoi(tmp) - mi;
290257
}
291-
292-
num - mi
293258
}
259+
260+
return num - mi;
294261
}
295262
```
296263

solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README_EN.md

Lines changed: 52 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ To get the maximum value, we need to find the first digit $s[i]$ in the string $
7676

7777
Finally, return the difference between the maximum and minimum values.
7878

79-
The time complexity is $O(\log n)$, and the space complexity is $O(\log n)$. Where $n$ is the size of the number $num$.
79+
The time complexity is $O(\log n)$, and the space complexity is $O(\log n)$. Where $n$ is the size of the number $\textit{num}$.
8080

8181
<!-- tabs:start -->
8282

@@ -174,14 +174,15 @@ func minMaxDifference(num int) int {
174174

175175
```ts
176176
function minMaxDifference(num: number): number {
177-
const s = num + '';
178-
const min = Number(s.replace(new RegExp(s[0], 'g'), '0'));
177+
const s = num.toString();
178+
const mi = +s.replaceAll(s[0], '0');
179179
for (const c of s) {
180180
if (c !== '9') {
181-
return Number(s.replace(new RegExp(c, 'g'), '9')) - min;
181+
const mx = +s.replaceAll(c, '9');
182+
return mx - mi;
182183
}
183184
}
184-
return num - min;
185+
return num - mi;
185186
}
186187
```
187188

@@ -191,103 +192,69 @@ function minMaxDifference(num: number): number {
191192
impl Solution {
192193
pub fn min_max_difference(num: i32) -> i32 {
193194
let s = num.to_string();
194-
let min = s
195-
.replace(char::from(s.as_bytes()[0]), "0")
196-
.parse::<i32>()
197-
.unwrap();
198-
for &c in s.as_bytes() {
199-
if c != b'9' {
200-
return s.replace(c, "9").parse().unwrap() - min;
195+
let mi = s.replace(s.chars().next().unwrap(), "0").parse::<i32>().unwrap();
196+
for c in s.chars() {
197+
if c != '9' {
198+
let mx = s.replace(c, "9").parse::<i32>().unwrap();
199+
return mx - mi;
201200
}
202201
}
203-
num - min
202+
num - mi
204203
}
205204
}
206205
```
207206

208-
#### C
207+
#### JavaScript
209208

210-
```c
211-
int getLen(int num) {
212-
int res = 0;
213-
while (num) {
214-
num /= 10;
215-
res++;
216-
}
217-
return res;
218-
}
219-
220-
int minMaxDifference(int num) {
221-
int n = getLen(num);
222-
int* nums = malloc(sizeof(int) * n);
223-
int t = num;
224-
for (int i = n - 1; i >= 0; i--) {
225-
nums[i] = t % 10;
226-
t /= 10;
227-
}
228-
int min = 0;
229-
for (int i = 0; i < n; i++) {
230-
min *= 10;
231-
if (nums[i] != nums[0]) {
232-
min += nums[i];
233-
}
234-
}
235-
int max = 0;
236-
int target = 10;
237-
for (int i = 0; i < n; i++) {
238-
max *= 10;
239-
if (target == 10 && nums[i] != 9) {
240-
target = nums[i];
209+
```js
210+
/**
211+
* @param {number} num
212+
* @return {number}
213+
*/
214+
var minMaxDifference = function (num) {
215+
const s = num.toString();
216+
const mi = +s.replaceAll(s[0], '0');
217+
for (const c of s) {
218+
if (c !== '9') {
219+
const mx = +s.replaceAll(c, '9');
220+
return mx - mi;
241221
}
242-
max += nums[i] == target ? 9 : nums[i];
243222
}
244-
free(nums);
245-
return max - min;
246-
}
223+
return num - mi;
224+
};
247225
```
248226

249-
<!-- tabs:end -->
250-
251-
<!-- solution:end -->
252-
253-
<!-- solution:start -->
254-
255-
### Solution 2
256-
257-
<!-- tabs:start -->
258-
259-
#### Rust
227+
#### C
260228

261-
```rust
262-
impl Solution {
263-
pub fn min_max_difference(num: i32) -> i32 {
264-
let mut s = num.to_string().into_bytes();
265-
let first = s[0];
266-
for i in 0..s.len() {
267-
if s[i] == first {
268-
s[i] = b'0';
269-
}
229+
```c
230+
int minMaxDifference(int num) {
231+
char s[12];
232+
sprintf(s, "%d", num);
233+
234+
int mi;
235+
{
236+
char tmp[12];
237+
char t = s[0];
238+
for (int i = 0; s[i]; i++) {
239+
tmp[i] = (s[i] == t) ? '0' : s[i];
270240
}
271-
let mi = String::from_utf8_lossy(&s).parse::<i32>().unwrap();
272-
273-
let mut t = num.to_string().into_bytes();
274-
for i in 0..t.len() {
275-
if t[i] != b'9' {
276-
let second = t[i];
277-
278-
for j in 0..t.len() {
279-
if t[j] == second {
280-
t[j] = b'9';
281-
}
282-
}
241+
tmp[strlen(s)] = '\0';
242+
mi = atoi(tmp);
243+
}
283244

284-
let mx = String::from_utf8_lossy(&t).parse::<i32>().unwrap();
285-
return mx - mi;
245+
for (int i = 0; s[i]; i++) {
246+
char c = s[i];
247+
if (c != '9') {
248+
char tmp[12];
249+
for (int j = 0; s[j]; j++) {
250+
tmp[j] = (s[j] == c) ? '9' : s[j];
286251
}
252+
tmp[strlen(s)] = '\0';
253+
return atoi(tmp) - mi;
287254
}
288-
289-
num - mi
290255
}
256+
257+
return num - mi;
291258
}
292259
```
293260

0 commit comments

Comments
 (0)