-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinterview_01.03_test.go
More file actions
57 lines (51 loc) · 1.1 KB
/
interview_01.03_test.go
File metadata and controls
57 lines (51 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,
//请使用字符数组实现,以便直接在数组上操作。)
//
//
//
// 示例 1:
//
//
//输入:"Mr John Smith ", 13
//输出:"Mr%20John%20Smith"
//
//
// 示例 2:
//
//
//输入:" ", 5
//输出:"%20%20%20%20%20"
//
//
//
//
// 提示:
//
//
// 字符串长度在 [0, 500000] 范围内。
//
// Related Topics 字符串
// 👍 39 👎 0
package leetcode
import (
"testing"
"unicode"
"github.com/stretchr/testify/assert"
)
func TestReplaceSpaces(t *testing.T) {
assert.Equal(t, "Mr%20John%20Smith", replaceSpaces("Mr John Smith", 13))
assert.Equal(t, "%20%20%20%20%20", replaceSpaces(" ", 5))
}
func replaceSpaces(s string, length int) string {
bytes := []byte{}
for i, e := range s {
if i < length {
if unicode.IsSpace(e) {
bytes = append(bytes, '%', '2', '0')
} else {
bytes = append(bytes, byte(e))
}
}
}
return string(bytes)
}