-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sword04.java
92 lines (84 loc) · 2.73 KB
/
Sword04.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package offer.ten;
/**
* Created by colin on 2017/3/16.
*/
public class Sword04 {
/* 替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
*/
public static void main(String[] arg) {
StringBuffer b = new StringBuffer("hello world ");
System.out.println(replaceSpace1(b));
}
public static String replaceSpace(StringBuffer str) {
if (str == null || str.length() < 1)
return "";
/*
char数组
*/
char[] test = str.toString().toCharArray();
StringBuffer b = new StringBuffer();
for (int i = 0; i < test.length; i++) {
if (test[i] == ' ') {
b.append("%20");
} else
b.append(test[i]);
}
return b.toString();
}
public static String replaceSpace1(StringBuffer str) {
if (str == null || str.length() < 1)
return "";
/*
直接利用charAt()
*/
StringBuilder b = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
//b.append("%20"); 添加字符串和添加字符效率不一样
b.append('%');
b.append('2');
b.append('0');
} else
b.append(str.charAt(i));
}
return b.toString();
}
public static String replaceSpace2(StringBuffer str) {
if (str == null || str.length() < 1)
return null;
/*
调用库函数,可能有很多人和我一样,根本不知道String库函数是啥
*/
// return new StringBuffer(str.toString().replaceAll(" ", "%20"));
return str.toString().replaceAll("\\s", "%20");
}
public static String replaceSpace3(StringBuffer str) {
/*
剑指offer的解法,两次遍历
*/
if (str == null || str.length() < 1)
return "";
//获得空格总数
int numofspace = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ')
numofspace += 2;
}
//开始迁移字符串并且添加字符
char[] a = new char[str.length() + numofspace];
int index = str.length() - 1;
int indexnew = a.length - 1;
for (; index >= 0; index--) {
if (str.charAt(index) == ' ') {
a[indexnew--] = '0';
a[indexnew--] = '2';
a[indexnew--] = '%';
} else {
a[indexnew--] = str.charAt(index);
}
}
return new String(a);
}
}