Skip to content

Commit 8929cae

Browse files
committed
feat: add solutions to lcci problem: No.01.03
No.01.03.String to URL
1 parent a709b99 commit 8929cae

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lcci/01.03.String to URL/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,41 @@ func replaceSpaces(S string, length int) string {
110110
}
111111
```
112112

113+
### **TypeScript**
114+
115+
```ts
116+
function replaceSpaces(S: string, length: number): string {
117+
return S.slice(0, length).replace(/\s/g, '%20');
118+
}
119+
```
120+
121+
### **Rust**
122+
123+
```rust
124+
impl Solution {
125+
pub fn replace_spaces(s: String, length: i32) -> String {
126+
s[..length as usize].replace(' ', "%20")
127+
}
128+
}
129+
```
130+
131+
```rust
132+
impl Solution {
133+
pub fn replace_spaces(s: String, length: i32) -> String {
134+
s.chars()
135+
.take(length as usize)
136+
.map(|c| {
137+
if c == ' ' {
138+
"%20".to_string()
139+
} else {
140+
c.to_string()
141+
}
142+
})
143+
.collect()
144+
}
145+
}
146+
```
147+
113148
### **...**
114149

115150
```

lcci/01.03.String to URL/README_EN.md

+35
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ func replaceSpaces(S string, length int) string {
119119
}
120120
```
121121

122+
### **TypeScript**
123+
124+
```ts
125+
function replaceSpaces(S: string, length: number): string {
126+
return S.slice(0, length).replace(/\s/g, '%20');
127+
}
128+
```
129+
130+
### **Rust**
131+
132+
```rust
133+
impl Solution {
134+
pub fn replace_spaces(s: String, length: i32) -> String {
135+
s[..length as usize].replace(' ', "%20")
136+
}
137+
}
138+
```
139+
140+
```rust
141+
impl Solution {
142+
pub fn replace_spaces(s: String, length: i32) -> String {
143+
s.chars()
144+
.take(length as usize)
145+
.map(|c| {
146+
if c == ' ' {
147+
"%20".to_string()
148+
} else {
149+
c.to_string()
150+
}
151+
})
152+
.collect()
153+
}
154+
}
155+
```
156+
122157
### **...**
123158

124159
```

lcci/01.03.String to URL/Solution.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn replace_spaces(s: String, length: i32) -> String {
3+
s[..length as usize].replace(' ', "%20")
4+
}
5+
}

lcci/01.03.String to URL/Solution.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function replaceSpaces(S: string, length: number): string {
2+
return S.slice(0, length).replace(/\s/g, '%20');
3+
}

0 commit comments

Comments
 (0)