Skip to content

Commit 8a8610f

Browse files
committedJan 31, 2023
feat: add solutions to lcof problem: No.05
1 parent b69c020 commit 8a8610f

File tree

2 files changed

+77
-72
lines changed

2 files changed

+77
-72
lines changed
 

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

+76-71
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@
1919

2020
## 解法
2121

22-
- 使用 `replace()` 替换。
23-
- 遍历添加。
22+
**方法一:字符串内置方法**
23+
24+
使用 `replace()` 方法。
25+
26+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
27+
28+
**方法二:遍历替换**
29+
30+
我们直接遍历字符串,遇到空格就替换成 `%20` 即可。
31+
32+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
2433

2534
<!-- tabs:start -->
2635

@@ -32,9 +41,16 @@ class Solution:
3241
return s.replace(' ', '%20')
3342
```
3443

35-
### **Java**
44+
```python
45+
class Solution:
46+
def replaceSpace(self, s: str) -> str:
47+
ans = []
48+
for c in s:
49+
ans.append('%20' if c == ' ' else c)
50+
return ''.join(ans)
51+
```
3652

37-
使用 replace:
53+
### **Java**
3854

3955
```java
4056
class Solution {
@@ -44,24 +60,59 @@ class Solution {
4460
}
4561
```
4662

47-
使用 StringBuilder:
48-
4963
```java
5064
class Solution {
5165
public String replaceSpace(String s) {
52-
StringBuilder sb = new StringBuilder();
53-
char[] chars = s.toCharArray();
54-
for (char c : chars) {
55-
sb.append(c == ' ' ? "%20" : c);
66+
StringBuilder ans = new StringBuilder();
67+
for (char c : s.toCharArray()) {
68+
ans.append(c == ' ' ? "%20" : c);
5669
}
57-
return sb.toString();
70+
return ans.toString();
5871
}
5972
}
6073
```
6174

62-
### **JavaScript**
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
string replaceSpace(string s) {
81+
string ans;
82+
for (char ch : s) {
83+
if (ch == ' ')
84+
ans += "%20";
85+
else
86+
ans += ch;
87+
}
88+
return ans;
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func replaceSpace(s string) string {
97+
return strings.Replace(s, " ", "%20", -1)
98+
}
99+
```
100+
101+
```go
102+
func replaceSpace(s string) string {
103+
ans := strings.Builder{}
104+
for _, c := range s {
105+
if c == ' ' {
106+
ans.WriteString("%20")
107+
} else {
108+
ans.WriteRune(c)
109+
}
110+
}
111+
return ans.String()
112+
}
113+
```
63114

64-
使用字符串内置方法:
115+
### **JavaScript**
65116

66117
```js
67118
/**
@@ -73,76 +124,38 @@ var replaceSpace = function (s) {
73124
};
74125
```
75126

76-
双指针:
77-
78127
```js
79128
/**
80-
* @param {string}
129+
* @param {string} s
81130
* @return {string}
82131
*/
83132
var replaceSpace = function (s) {
84-
s = s.split('');
85-
let emptyNum = 0;
86-
for (let i = 0; i < s.length; i++) {
87-
if (s[i] === ' ') {
88-
emptyNum++;
89-
}
90-
}
91-
let p1 = s.length - 1;
92-
let p2 = p1 + 2 * emptyNum;
93-
while (p1 >= 0 && p2 > p1) {
94-
if (s[p1] === ' ') {
95-
s[p2] = '0';
96-
s[--p2] = '2';
97-
s[--p2] = '%';
98-
} else {
99-
s[p2] = s[p1];
100-
}
101-
p1--;
102-
p2--;
103-
}
104-
return s.join('');
133+
return s.replace(/\s/g, '%20');
105134
};
106135
```
107136

108-
### **Go**
109-
110-
```go
111-
func replaceSpace(s string) string {
112-
return strings.Replace(s, " ", "%20", -1)
113-
}
114-
```
115-
116-
### **C++**
117-
118-
```cpp
119-
class Solution {
120-
public:
121-
string replaceSpace(string s) {
122-
string ans;
123-
for (char ch : s) {
124-
if (ch == ' ')
125-
ans += "%20";
126-
else
127-
ans += ch;
128-
}
129-
return ans;
137+
```js
138+
/**
139+
* @param {string} s
140+
* @return {string}
141+
*/
142+
var replaceSpace = function (s) {
143+
const ans = [];
144+
for (const c of s) {
145+
ans.push(c === ' ' ? '%20' : c);
130146
}
147+
return ans.join('');
131148
};
132149
```
133150

134151
### **TypeScript**
135152

136-
使用 `replace()`:
137-
138153
```ts
139154
function replaceSpace(s: string): string {
140155
return s.replace(/\s/g, '%20');
141156
}
142157
```
143158

144-
遍历添加:
145-
146159
```ts
147160
function replaceSpace(s: string): string {
148161
const strArr = [];
@@ -155,8 +168,6 @@ function replaceSpace(s: string): string {
155168

156169
### **Rust**
157170

158-
使用 `replace()`
159-
160171
```rust
161172
impl Solution {
162173
pub fn replace_space(s: String) -> String {
@@ -165,8 +176,6 @@ impl Solution {
165176
}
166177
```
167178

168-
遍历添加:
169-
170179
```rust
171180
impl Solution {
172181
pub fn replace_space(s: String) -> String {
@@ -185,8 +194,6 @@ impl Solution {
185194

186195
### **C#**
187196

188-
使用 `Replace()`
189-
190197
```cs
191198
public class Solution {
192199
public string ReplaceSpace(string s) {
@@ -195,8 +202,6 @@ public class Solution {
195202
}
196203
```
197204

198-
遍历添加:
199-
200205
```cs
201206
public class Solution {
202207
public string ReplaceSpace(string s) {

‎lcof/面试题05. 替换空格/Solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
* @return {string}
44
*/
55
var replaceSpace = function (s) {
6-
return s.split(' ').join('%20');
6+
return s.replace(/\s/g, '%20');
77
};

0 commit comments

Comments
 (0)
Please sign in to comment.