Skip to content

Commit 998e4e3

Browse files
authored
feat: add rust solution to lcof problem: No.05 (doocs#677)
面试题 05. 替换空格
1 parent 55fb4c1 commit 998e4e3

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lcof/面试题05. 替换空格/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ function replaceSpace(s: string): string {
151151
}
152152
```
153153

154+
### **Rust**
155+
156+
- 使用 `replace()`
157+
158+
```rust
159+
impl Solution {
160+
pub fn replace_space(s: String) -> String {
161+
s.replace(' ', "%20")
162+
}
163+
}
164+
```
165+
166+
- 遍历添加
167+
168+
```rust
169+
impl Solution {
170+
pub fn replace_space(s: String) -> String {
171+
let mut result = String::new();
172+
for c in s.chars() {
173+
if c == ' ' {
174+
result.push_str("%20");
175+
} else {
176+
result.push(c);
177+
}
178+
}
179+
result
180+
}
181+
}
182+
```
183+
154184
### **...**
155185

156186
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn replace_space(s: String) -> String {
3+
s.replace(' ', "%20")
4+
}
5+
}

0 commit comments

Comments
 (0)