替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
1
2
2
/**
* @param {string} s
* @return {string}
*/
var replaceSpace = function(s) {
let len = s.length
let arr = new Array(len * 3).fill(0)
let size = 0
for(let i = 0; i < len; i++) {
if(s[i] === ' ') {
arr[size] = "%"
arr[size + 1] = "2"
arr[size + 2] = "0"
size += 3
} else {
arr[size] = s[i]
size++
}
}
return arr.slice(0, size).join("")
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof