Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 945c8bf

Browse files
committed
feat: add solutions to lc problem: No.2566
No.2566.Maximum Difference by Remapping a Digit
1 parent 4433a65 commit 945c8bf

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,82 @@ func minMaxDifference(num int) int {
162162
}
163163
```
164164

165+
### **TypeScript**
166+
167+
```ts
168+
function minMaxDifference(num: number): number {
169+
const s = num + '';
170+
const min = Number(s.replace(new RegExp(s[0], 'g'), '0'));
171+
for (const c of s) {
172+
if (c !== '9') {
173+
return Number(s.replace(new RegExp(c, 'g'), '9')) - min;
174+
}
175+
}
176+
return num - min;
177+
}
178+
```
179+
180+
### **Rust**
181+
182+
```rust
183+
impl Solution {
184+
pub fn min_max_difference(num: i32) -> i32 {
185+
let s = num.to_string();
186+
let min = s
187+
.replace(char::from(s.as_bytes()[0]), "0")
188+
.parse::<i32>()
189+
.unwrap();
190+
for &c in s.as_bytes() {
191+
if c != b'9' {
192+
return s.replace(c, "9").parse().unwrap() - min;
193+
}
194+
}
195+
num - min
196+
}
197+
}
198+
```
199+
200+
### **C**
201+
202+
```c
203+
int getLen(int num) {
204+
int res = 0;
205+
while (num) {
206+
num /= 10;
207+
res++;
208+
}
209+
return res;
210+
}
211+
212+
int minMaxDifference(int num) {
213+
int n = getLen(num);
214+
int *nums = malloc(sizeof(int) * n);
215+
int t = num;
216+
for (int i = n - 1; i >= 0; i--) {
217+
nums[i] = t % 10;
218+
t /= 10;
219+
}
220+
int min = 0;
221+
for (int i = 0; i < n; i++) {
222+
min *= 10;
223+
if (nums[i] != nums[0]) {
224+
min += nums[i];
225+
}
226+
}
227+
int max = 0;
228+
int target = 10;
229+
for (int i = 0; i < n; i++) {
230+
max *= 10;
231+
if (target == 10 && nums[i] != 9) {
232+
target = nums[i];
233+
}
234+
max += nums[i] == target ? 9 : nums[i];
235+
}
236+
free(nums);
237+
return max - min;
238+
}
239+
```
240+
165241
### **...**
166242
167243
```

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,82 @@ func minMaxDifference(num int) int {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function minMaxDifference(num: number): number {
147+
const s = num + '';
148+
const min = Number(s.replace(new RegExp(s[0], 'g'), '0'));
149+
for (const c of s) {
150+
if (c !== '9') {
151+
return Number(s.replace(new RegExp(c, 'g'), '9')) - min;
152+
}
153+
}
154+
return num - min;
155+
}
156+
```
157+
158+
### **Rust**
159+
160+
```rust
161+
impl Solution {
162+
pub fn min_max_difference(num: i32) -> i32 {
163+
let s = num.to_string();
164+
let min = s
165+
.replace(char::from(s.as_bytes()[0]), "0")
166+
.parse::<i32>()
167+
.unwrap();
168+
for &c in s.as_bytes() {
169+
if c != b'9' {
170+
return s.replace(c, "9").parse().unwrap() - min;
171+
}
172+
}
173+
num - min
174+
}
175+
}
176+
```
177+
178+
### **C**
179+
180+
```c
181+
int getLen(int num) {
182+
int res = 0;
183+
while (num) {
184+
num /= 10;
185+
res++;
186+
}
187+
return res;
188+
}
189+
190+
int minMaxDifference(int num) {
191+
int n = getLen(num);
192+
int *nums = malloc(sizeof(int) * n);
193+
int t = num;
194+
for (int i = n - 1; i >= 0; i--) {
195+
nums[i] = t % 10;
196+
t /= 10;
197+
}
198+
int min = 0;
199+
for (int i = 0; i < n; i++) {
200+
min *= 10;
201+
if (nums[i] != nums[0]) {
202+
min += nums[i];
203+
}
204+
}
205+
int max = 0;
206+
int target = 10;
207+
for (int i = 0; i < n; i++) {
208+
max *= 10;
209+
if (target == 10 && nums[i] != 9) {
210+
target = nums[i];
211+
}
212+
max += nums[i] == target ? 9 : nums[i];
213+
}
214+
free(nums);
215+
return max - min;
216+
}
217+
```
218+
143219
### **...**
144220
145221
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
int getLen(int num) {
2+
int res = 0;
3+
while (num) {
4+
num /= 10;
5+
res++;
6+
}
7+
return res;
8+
}
9+
10+
int minMaxDifference(int num) {
11+
int n = getLen(num);
12+
int *nums = malloc(sizeof(int) * n);
13+
int t = num;
14+
for (int i = n - 1; i >= 0; i--) {
15+
nums[i] = t % 10;
16+
t /= 10;
17+
}
18+
int min = 0;
19+
for (int i = 0; i < n; i++) {
20+
min *= 10;
21+
if (nums[i] != nums[0]) {
22+
min += nums[i];
23+
}
24+
}
25+
int max = 0;
26+
int target = 10;
27+
for (int i = 0; i < n; i++) {
28+
max *= 10;
29+
if (target == 10 && nums[i] != 9) {
30+
target = nums[i];
31+
}
32+
max += nums[i] == target ? 9 : nums[i];
33+
}
34+
free(nums);
35+
return max - min;
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn min_max_difference(num: i32) -> i32 {
3+
let s = num.to_string();
4+
let min = s
5+
.replace(char::from(s.as_bytes()[0]), "0")
6+
.parse::<i32>()
7+
.unwrap();
8+
for &c in s.as_bytes() {
9+
if c != b'9' {
10+
return s.replace(c, "9").parse().unwrap() - min;
11+
}
12+
}
13+
num - min
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minMaxDifference(num: number): number {
2+
const s = num + '';
3+
const min = Number(s.replace(new RegExp(s[0], 'g'), '0'));
4+
for (const c of s) {
5+
if (c !== '9') {
6+
return Number(s.replace(new RegExp(c, 'g'), '9')) - min;
7+
}
8+
}
9+
return num - min;
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /