Skip to content

Commit a82310e

Browse files
committed
feat: add solutions to lcof problem: No.13
面试题13. 机器人的运动范围
1 parent a0d9515 commit a82310e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

lcof/面试题13. 机器人的运动范围/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,60 @@ public:
146146
};
147147
```
148148
149+
### **TypeScript**
150+
151+
```ts
152+
function movingCount(m: number, n: number, k: number): number {
153+
const set = new Set();
154+
const dfs = (y: number, x: number) => {
155+
if (y === m || x === n || set.has(`${y},${x}`)) {
156+
return;
157+
}
158+
let count = 0;
159+
const str = `${y}${x}`;
160+
for (const c of str) {
161+
count += Number(c);
162+
}
163+
if (count <= k) {
164+
set.add(`${y},${x}`);
165+
dfs(y + 1, x);
166+
dfs(y, x + 1);
167+
}
168+
};
169+
dfs(0, 0);
170+
return set.size;
171+
}
172+
```
173+
174+
### **Rust**
175+
176+
```rust
177+
use std::collections::{HashSet, VecDeque};
178+
179+
impl Solution {
180+
pub fn moving_count(m: i32, n: i32, k: i32) -> i32 {
181+
let mut deque = VecDeque::new();
182+
let mut set = HashSet::new();
183+
deque.push_back([0, 0]);
184+
while let Some([y, x]) = deque.pop_front() {
185+
if y < m && x < n && !set.contains(&format!("{},{}", y, x)) {
186+
let str = format!("{}{}", y, x);
187+
let mut count = 0;
188+
for c in str.chars() {
189+
count += c.to_string().parse::<i32>().unwrap();
190+
}
191+
if count <= k {
192+
set.insert(format!("{},{}", y, x));
193+
deque.push_back([y + 1, x]);
194+
deque.push_back([y, x + 1]);
195+
}
196+
}
197+
}
198+
set.len() as i32
199+
}
200+
}
201+
```
202+
149203
### **...**
150204

151205
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
impl Solution {
4+
pub fn moving_count(m: i32, n: i32, k: i32) -> i32 {
5+
let mut deque = VecDeque::new();
6+
let mut set = HashSet::new();
7+
deque.push_back([0, 0]);
8+
while let Some([y, x]) = deque.pop_front() {
9+
if y < m && x < n && !set.contains(&format!("{},{}", y, x)) {
10+
let str = format!("{}{}", y, x);
11+
let mut count = 0;
12+
for c in str.chars() {
13+
count += c.to_string().parse::<i32>().unwrap();
14+
}
15+
if count <= k {
16+
set.insert(format!("{},{}", y, x));
17+
deque.push_back([y + 1, x]);
18+
deque.push_back([y, x + 1]);
19+
}
20+
}
21+
}
22+
set.len() as i32
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function movingCount(m: number, n: number, k: number): number {
2+
const set = new Set();
3+
const dfs = (y: number, x: number) => {
4+
if (y === m || x === n || set.has(`${y},${x}`)) {
5+
return;
6+
}
7+
let count = 0;
8+
const str = `${y}${x}`;
9+
for (const c of str) {
10+
count += Number(c);
11+
}
12+
if (count <= k) {
13+
set.add(`${y},${x}`);
14+
dfs(y + 1, x);
15+
dfs(y, x + 1);
16+
}
17+
};
18+
dfs(0, 0);
19+
return set.size;
20+
}

0 commit comments

Comments
 (0)