Skip to content

Commit f2fcef1

Browse files
committed
feat: add solutions to lc problem: No.1946
No.1946.Largest Number After Mutating Substring
1 parent d108c2e commit f2fcef1

File tree

6 files changed

+149
-63
lines changed

6 files changed

+149
-63
lines changed

solution/1900-1999/1946.Largest Number After Mutating Substring/README.md

+57-21
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:贪心**
64+
65+
从左到右遍历字符串 `num`,找到第一个比 `change` 中对应数字小的数字,然后将其替换为 `change` 中对应的数字,直到遇到比 `change` 中对应数字大的数字,停止替换。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `num` 的长度。
68+
6369
<!-- tabs:start -->
6470

6571
### **Python3**
@@ -69,17 +75,14 @@
6975
```python
7076
class Solution:
7177
def maximumNumber(self, num: str, change: List[int]) -> str:
72-
find = False
73-
nums = list(num)
74-
for i, c in enumerate(num):
75-
if int(c) < change[int(c)]:
76-
nums[i] = str(change[int(c)])
77-
find = True
78-
elif find and int(c) == change[int(c)]:
79-
continue
80-
elif find:
78+
s = list(num)
79+
for i, c in enumerate(s):
80+
if change[int(c)] > int(c):
81+
while i < len(s) and int(s[i]) <= change[int(s[i])]:
82+
s[i] = str(change[int(s[i])])
83+
i += 1
8184
break
82-
return ''.join(nums)
85+
return ''.join(s)
8386
```
8487

8588
### **Java**
@@ -89,21 +92,54 @@ class Solution:
8992
```java
9093
class Solution {
9194
public String maximumNumber(String num, int[] change) {
92-
boolean find = false;
93-
char[] nums = num.toCharArray();
94-
for (int i = 0; i < num.length(); ++i) {
95-
int c = num.charAt(i) - '0';
96-
if (c < change[c]) {
97-
nums[i] = (char) ('0' + change[c]);
98-
find = true;
99-
} else if (find && c == change[c]) {
100-
continue;
101-
} else if (find) {
95+
char[] s = num.toCharArray();
96+
for (int i = 0; i < s.length; ++i) {
97+
if (change[s[i] - '0'] > s[i] - '0') {
98+
for (; i < s.length && s[i] - '0' <= change[s[i] - '0']; ++i) {
99+
s[i] = (char) (change[s[i] - '0'] + '0');
100+
}
101+
break;
102+
}
103+
}
104+
return String.valueOf(s);
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
string maximumNumber(string num, vector<int>& change) {
115+
int n = num.size();
116+
for (int i = 0; i < n; ++i) {
117+
if (change[num[i] - '0'] > num[i] - '0') {
118+
for (; i < n && change[num[i] - '0'] >= num[i] - '0'; ++i) {
119+
num[i] = change[num[i] - '0'] + '0';
120+
}
102121
break;
103122
}
104123
}
105-
return new String(nums);
124+
return num;
106125
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func maximumNumber(num string, change []int) string {
133+
s := []byte(num)
134+
for i, c := range num {
135+
if change[c-'0'] > int(c-'0') {
136+
for ; i < len(s) && change[s[i]-'0'] >= int(s[i]-'0'); i++ {
137+
s[i] = byte(change[s[i]-'0']) + '0'
138+
}
139+
break
140+
}
141+
}
142+
return string(s)
107143
}
108144
```
109145

solution/1900-1999/1946.Largest Number After Mutating Substring/README_EN.md

+51-21
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,72 @@ Thus, &quot;<u>021</u>&quot; becomes &quot;<u>934</u>&quot;.
6464
```python
6565
class Solution:
6666
def maximumNumber(self, num: str, change: List[int]) -> str:
67-
find = False
68-
nums = list(num)
69-
for i, c in enumerate(num):
70-
if int(c) < change[int(c)]:
71-
nums[i] = str(change[int(c)])
72-
find = True
73-
elif find and int(c) == change[int(c)]:
74-
continue
75-
elif find:
67+
s = list(num)
68+
for i, c in enumerate(s):
69+
if change[int(c)] > int(c):
70+
while i < len(s) and int(s[i]) <= change[int(s[i])]:
71+
s[i] = str(change[int(s[i])])
72+
i += 1
7673
break
77-
return ''.join(nums)
74+
return ''.join(s)
7875
```
7976

8077
### **Java**
8178

8279
```java
8380
class Solution {
8481
public String maximumNumber(String num, int[] change) {
85-
boolean find = false;
86-
char[] nums = num.toCharArray();
87-
for (int i = 0; i < num.length(); ++i) {
88-
int c = num.charAt(i) - '0';
89-
if (c < change[c]) {
90-
nums[i] = (char) ('0' + change[c]);
91-
find = true;
92-
} else if (find && c == change[c]) {
93-
continue;
94-
} else if (find) {
82+
char[] s = num.toCharArray();
83+
for (int i = 0; i < s.length; ++i) {
84+
if (change[s[i] - '0'] > s[i] - '0') {
85+
for (; i < s.length && s[i] - '0' <= change[s[i] - '0']; ++i) {
86+
s[i] = (char) (change[s[i] - '0'] + '0');
87+
}
9588
break;
9689
}
9790
}
98-
return new String(nums);
91+
return String.valueOf(s);
9992
}
10093
}
10194
```
10295

96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
string maximumNumber(string num, vector<int>& change) {
102+
int n = num.size();
103+
for (int i = 0; i < n; ++i) {
104+
if (change[num[i] - '0'] > num[i] - '0') {
105+
for (; i < n && change[num[i] - '0'] >= num[i] - '0'; ++i) {
106+
num[i] = change[num[i] - '0'] + '0';
107+
}
108+
break;
109+
}
110+
}
111+
return num;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func maximumNumber(num string, change []int) string {
120+
s := []byte(num)
121+
for i, c := range num {
122+
if change[c-'0'] > int(c-'0') {
123+
for ; i < len(s) && change[s[i]-'0'] >= int(s[i]-'0'); i++ {
124+
s[i] = byte(change[s[i]-'0']) + '0'
125+
}
126+
break
127+
}
128+
}
129+
return string(s)
130+
}
131+
```
132+
103133
### **...**
104134

105135
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string maximumNumber(string num, vector<int>& change) {
4+
int n = num.size();
5+
for (int i = 0; i < n; ++i) {
6+
if (change[num[i] - '0'] > num[i] - '0') {
7+
for (; i < n && change[num[i] - '0'] >= num[i] - '0'; ++i) {
8+
num[i] = change[num[i] - '0'] + '0';
9+
}
10+
break;
11+
}
12+
}
13+
return num;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func maximumNumber(num string, change []int) string {
2+
s := []byte(num)
3+
for i, c := range num {
4+
if change[c-'0'] > int(c-'0') {
5+
for ; i < len(s) && change[s[i]-'0'] >= int(s[i]-'0'); i++ {
6+
s[i] = byte(change[s[i]-'0']) + '0'
7+
}
8+
break
9+
}
10+
}
11+
return string(s)
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
class Solution {
22
public String maximumNumber(String num, int[] change) {
3-
boolean find = false;
4-
char[] nums = num.toCharArray();
5-
for (int i = 0; i < num.length(); ++i) {
6-
int c = num.charAt(i) - '0';
7-
if (c < change[c]) {
8-
nums[i] = (char) ('0' + change[c]);
9-
find = true;
10-
} else if (find && c == change[c]) {
11-
continue;
12-
} else if (find) {
3+
char[] s = num.toCharArray();
4+
for (int i = 0; i < s.length; ++i) {
5+
if (change[s[i] - '0'] > s[i] - '0') {
6+
for (; i < s.length && s[i] - '0' <= change[s[i] - '0']; ++i) {
7+
s[i] = (char) (change[s[i] - '0'] + '0');
8+
}
139
break;
1410
}
1511
}
16-
return new String(nums);
12+
return String.valueOf(s);
1713
}
1814
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution:
22
def maximumNumber(self, num: str, change: List[int]) -> str:
3-
find = False
4-
nums = list(num)
5-
for i, c in enumerate(num):
6-
if int(c) < change[int(c)]:
7-
nums[i] = str(change[int(c)])
8-
find = True
9-
elif find and int(c) == change[int(c)]:
10-
continue
11-
elif find:
3+
s = list(num)
4+
for i, c in enumerate(s):
5+
if change[int(c)] > int(c):
6+
while i < len(s) and int(s[i]) <= change[int(s[i])]:
7+
s[i] = str(change[int(s[i])])
8+
i += 1
129
break
13-
return ''.join(nums)
10+
return ''.join(s)

0 commit comments

Comments
 (0)