Skip to content

Commit fae5f45

Browse files
committed
feat: add solutions to lc problems: No.1844, 2120
- No.1844.Replace All Digits with Characters - No.2120.Execution of All Suffix Instructions Staying in a Grid
1 parent 38abece commit fae5f45

File tree

10 files changed

+381
-0
lines changed

10 files changed

+381
-0
lines changed

solution/1800-1899/1844.Replace All Digits with Characters/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,48 @@ class Solution {
8484
}
8585
```
8686

87+
### **TypeScript**
88+
89+
```ts
90+
function replaceDigits(s: string): string {
91+
const n = s.length;
92+
const ans = [...s];
93+
for (let i = 1; i < n; i += 2) {
94+
ans[i] = String.fromCharCode(ans[i - 1].charCodeAt(0) + Number(ans[i]));
95+
}
96+
return ans.join('');
97+
}
98+
```
99+
100+
### **Rust**
101+
102+
```rust
103+
impl Solution {
104+
pub fn replace_digits(s: String) -> String {
105+
let n = s.len();
106+
let mut ans = s.into_bytes();
107+
let mut i = 1;
108+
while i < n {
109+
ans[i] = ans[i - 1] + (ans[i] - b'0');
110+
i += 2;
111+
}
112+
ans.into_iter().map(char::from).collect()
113+
}
114+
}
115+
```
116+
117+
### **C**
118+
119+
```c
120+
char *replaceDigits(char *s) {
121+
int n = strlen(s);
122+
for (int i = 1; i < n; i += 2) {
123+
s[i] = s[i - 1] + s[i] - '0';
124+
}
125+
return s;
126+
}
127+
```
128+
87129
### **...**
88130
89131
```

solution/1800-1899/1844.Replace All Digits with Characters/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,48 @@ class Solution {
7676
}
7777
```
7878

79+
### **TypeScript**
80+
81+
```ts
82+
function replaceDigits(s: string): string {
83+
const n = s.length;
84+
const ans = [...s];
85+
for (let i = 1; i < n; i += 2) {
86+
ans[i] = String.fromCharCode(ans[i - 1].charCodeAt(0) + Number(ans[i]));
87+
}
88+
return ans.join('');
89+
}
90+
```
91+
92+
### **Rust**
93+
94+
```rust
95+
impl Solution {
96+
pub fn replace_digits(s: String) -> String {
97+
let n = s.len();
98+
let mut ans = s.into_bytes();
99+
let mut i = 1;
100+
while i < n {
101+
ans[i] = ans[i - 1] + (ans[i] - b'0');
102+
i += 2;
103+
}
104+
ans.into_iter().map(char::from).collect()
105+
}
106+
}
107+
```
108+
109+
### **C**
110+
111+
```c
112+
char *replaceDigits(char *s) {
113+
int n = strlen(s);
114+
for (int i = 1; i < n; i += 2) {
115+
s[i] = s[i - 1] + s[i] - '0';
116+
}
117+
return s;
118+
}
119+
```
120+
79121
### **...**
80122
81123
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
char *replaceDigits(char *s) {
2+
int n = strlen(s);
3+
for (int i = 1; i < n; i += 2) {
4+
s[i] = s[i - 1] + s[i] - '0';
5+
}
6+
return s;
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn replace_digits(s: String) -> String {
3+
let n = s.len();
4+
let mut ans = s.into_bytes();
5+
let mut i = 1;
6+
while i < n {
7+
ans[i] = ans[i - 1] + (ans[i] - b'0');
8+
i += 2;
9+
}
10+
ans.into_iter().map(char::from).collect()
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function replaceDigits(s: string): string {
2+
const n = s.length;
3+
const ans = [...s];
4+
for (let i = 1; i < n; i += 2) {
5+
ans[i] = String.fromCharCode(ans[i - 1].charCodeAt(0) + Number(ans[i]));
6+
}
7+
return ans.join('');
8+
}

solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,100 @@ func executeInstructions(n int, startPos []int, s string) []int {
206206
<!-- 这里可写当前语言的特殊实现逻辑 -->
207207

208208
```ts
209+
function executeInstructions(
210+
n: number,
211+
startPos: number[],
212+
s: string,
213+
): number[] {
214+
const m = s.length;
215+
const ans = new Array(m);
216+
for (let i = 0; i < m; i++) {
217+
let [y, x] = startPos;
218+
let j: number;
219+
for (j = i; j < m; j++) {
220+
const c = s[j];
221+
if (c === 'U') {
222+
y--;
223+
} else if (c === 'D') {
224+
y++;
225+
} else if (c === 'L') {
226+
x--;
227+
} else {
228+
x++;
229+
}
230+
if (y === -1 || y === n || x === -1 || x === n) {
231+
break;
232+
}
233+
}
234+
ans[i] = j - i;
235+
}
236+
return ans;
237+
}
238+
```
239+
240+
### **Rust**
241+
242+
```rust
243+
impl Solution {
244+
pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> {
245+
let s = s.as_bytes();
246+
let m = s.len();
247+
let mut ans = vec![0; m];
248+
for i in 0..m {
249+
let mut y = start_pos[0];
250+
let mut x = start_pos[1];
251+
let mut j = i;
252+
while j < m {
253+
match s[j] {
254+
b'U' => y -= 1,
255+
b'D' => y += 1,
256+
b'L' => x -= 1,
257+
_ => x += 1,
258+
}
259+
if y == -1 || y == n || x == -1 || x == n {
260+
break;
261+
}
262+
j += 1;
263+
}
264+
ans[i] = (j - i) as i32;
265+
}
266+
ans
267+
}
268+
}
269+
```
209270

271+
### **C**
272+
273+
```c
274+
/**
275+
* Note: The returned array must be malloced, assume caller calls free().
276+
*/
277+
int *executeInstructions(int n, int *startPos, int startPosSize, char *s, int *returnSize) {
278+
int m = strlen(s);
279+
int *ans = malloc(sizeof(int) * m);
280+
for (int i = 0; i < m; i++) {
281+
int y = startPos[0];
282+
int x = startPos[1];
283+
int j = i;
284+
for (j = i; j < m; j++) {
285+
if (s[j] == 'U') {
286+
y--;
287+
} else if (s[j] == 'D') {
288+
y++;
289+
} else if (s[j] == 'L') {
290+
x--;
291+
} else {
292+
x++;
293+
}
294+
if (y == -1 || y == n || x == -1 || x == n) {
295+
break;
296+
}
297+
}
298+
ans[i] = j - i;
299+
}
300+
*returnSize = m;
301+
return ans;
302+
}
210303
```
211304
212305
### **...**

solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,100 @@ func executeInstructions(n int, startPos []int, s string) []int {
186186
### **TypeScript**
187187

188188
```ts
189+
function executeInstructions(
190+
n: number,
191+
startPos: number[],
192+
s: string,
193+
): number[] {
194+
const m = s.length;
195+
const ans = new Array(m);
196+
for (let i = 0; i < m; i++) {
197+
let [y, x] = startPos;
198+
let j: number;
199+
for (j = i; j < m; j++) {
200+
const c = s[j];
201+
if (c === 'U') {
202+
y--;
203+
} else if (c === 'D') {
204+
y++;
205+
} else if (c === 'L') {
206+
x--;
207+
} else {
208+
x++;
209+
}
210+
if (y === -1 || y === n || x === -1 || x === n) {
211+
break;
212+
}
213+
}
214+
ans[i] = j - i;
215+
}
216+
return ans;
217+
}
218+
```
219+
220+
### **Rust**
221+
222+
```rust
223+
impl Solution {
224+
pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> {
225+
let s = s.as_bytes();
226+
let m = s.len();
227+
let mut ans = vec![0; m];
228+
for i in 0..m {
229+
let mut y = start_pos[0];
230+
let mut x = start_pos[1];
231+
let mut j = i;
232+
while j < m {
233+
match s[j] {
234+
b'U' => y -= 1,
235+
b'D' => y += 1,
236+
b'L' => x -= 1,
237+
_ => x += 1,
238+
}
239+
if y == -1 || y == n || x == -1 || x == n {
240+
break;
241+
}
242+
j += 1;
243+
}
244+
ans[i] = (j - i) as i32;
245+
}
246+
ans
247+
}
248+
}
249+
```
250+
251+
### **C**
189252

253+
```c
254+
/**
255+
* Note: The returned array must be malloced, assume caller calls free().
256+
*/
257+
int *executeInstructions(int n, int *startPos, int startPosSize, char *s, int *returnSize) {
258+
int m = strlen(s);
259+
int *ans = malloc(sizeof(int) * m);
260+
for (int i = 0; i < m; i++) {
261+
int y = startPos[0];
262+
int x = startPos[1];
263+
int j = i;
264+
for (j = i; j < m; j++) {
265+
if (s[j] == 'U') {
266+
y--;
267+
} else if (s[j] == 'D') {
268+
y++;
269+
} else if (s[j] == 'L') {
270+
x--;
271+
} else {
272+
x++;
273+
}
274+
if (y == -1 || y == n || x == -1 || x == n) {
275+
break;
276+
}
277+
}
278+
ans[i] = j - i;
279+
}
280+
*returnSize = m;
281+
return ans;
282+
}
190283
```
191284
192285
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *executeInstructions(int n, int *startPos, int startPosSize, char *s, int *returnSize) {
5+
int m = strlen(s);
6+
int *ans = malloc(sizeof(int) * m);
7+
for (int i = 0; i < m; i++) {
8+
int y = startPos[0];
9+
int x = startPos[1];
10+
int j = i;
11+
for (j = i; j < m; j++) {
12+
if (s[j] == 'U') {
13+
y--;
14+
} else if (s[j] == 'D') {
15+
y++;
16+
} else if (s[j] == 'L') {
17+
x--;
18+
} else {
19+
x++;
20+
}
21+
if (y == -1 || y == n || x == -1 || x == n) {
22+
break;
23+
}
24+
}
25+
ans[i] = j - i;
26+
}
27+
*returnSize = m;
28+
return ans;
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> {
3+
let s = s.as_bytes();
4+
let m = s.len();
5+
let mut ans = vec![0; m];
6+
for i in 0..m {
7+
let mut y = start_pos[0];
8+
let mut x = start_pos[1];
9+
let mut j = i;
10+
while j < m {
11+
match s[j] {
12+
b'U' => y -= 1,
13+
b'D' => y += 1,
14+
b'L' => x -= 1,
15+
_ => x += 1,
16+
}
17+
if y == -1 || y == n || x == -1 || x == n {
18+
break;
19+
}
20+
j += 1;
21+
}
22+
ans[i] = (j - i) as i32;
23+
}
24+
ans
25+
}
26+
}

0 commit comments

Comments
 (0)