19
19
20
20
## 解法
21
21
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$ 为字符串长度。
24
33
25
34
<!-- tabs:start -->
26
35
@@ -32,9 +41,16 @@ class Solution:
32
41
return s.replace(' ' , ' %20' )
33
42
```
34
43
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
+ ```
36
52
37
- 使用 replace:
53
+ ### ** Java **
38
54
39
55
``` java
40
56
class Solution {
@@ -44,24 +60,59 @@ class Solution {
44
60
}
45
61
```
46
62
47
- 使用 StringBuilder:
48
-
49
63
``` java
50
64
class Solution {
51
65
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);
56
69
}
57
- return sb . toString();
70
+ return ans . toString();
58
71
}
59
72
}
60
73
```
61
74
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
+ ```
63
114
64
- 使用字符串内置方法:
115
+ ### ** JavaScript **
65
116
66
117
``` js
67
118
/**
@@ -73,76 +124,38 @@ var replaceSpace = function (s) {
73
124
};
74
125
```
75
126
76
- 双指针:
77
-
78
127
``` js
79
128
/**
80
- * @param {string}
129
+ * @param {string} s
81
130
* @return {string}
82
131
*/
83
132
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' );
105
134
};
106
135
```
107
136
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);
130
146
}
147
+ return ans .join (' ' );
131
148
};
132
149
```
133
150
134
151
### ** TypeScript**
135
152
136
- 使用 `replace()`:
137
-
138
153
``` ts
139
154
function replaceSpace(s : string ): string {
140
155
return s .replace (/ \s / g , ' %20' );
141
156
}
142
157
```
143
158
144
- 遍历添加:
145
-
146
159
``` ts
147
160
function replaceSpace(s : string ): string {
148
161
const strArr = [];
@@ -155,8 +168,6 @@ function replaceSpace(s: string): string {
155
168
156
169
### ** Rust**
157
170
158
- 使用 ` replace() ` :
159
-
160
171
``` rust
161
172
impl Solution {
162
173
pub fn replace_space (s : String ) -> String {
@@ -165,8 +176,6 @@ impl Solution {
165
176
}
166
177
```
167
178
168
- 遍历添加:
169
-
170
179
``` rust
171
180
impl Solution {
172
181
pub fn replace_space (s : String ) -> String {
@@ -185,8 +194,6 @@ impl Solution {
185
194
186
195
### ** C#**
187
196
188
- 使用 ` Replace() ` :
189
-
190
197
``` cs
191
198
public class Solution {
192
199
public string ReplaceSpace (string s ) {
@@ -195,8 +202,6 @@ public class Solution {
195
202
}
196
203
```
197
204
198
- 遍历添加:
199
-
200
205
``` cs
201
206
public class Solution {
202
207
public string ReplaceSpace (string s ) {
0 commit comments