-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetSpaceString.java
More file actions
51 lines (51 loc) · 1.75 KB
/
SetSpaceString.java
File metadata and controls
51 lines (51 loc) · 1.75 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
//这是新开辟了一个字符串来做替换。
public class Solution {
public String replaceSpace(StringBuffer str) {
if(str==null)
return null;
StringBuilder nstr = new StringBuilder();
for (int i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')
{
nstr.append('%');
nstr.append('2');
nstr.append('0');
}else
{
nstr.append(str.charAt(i));
}
}
return nstr.toString();
}
}
//在原有的字符串基础上做替换,Stringbuilder的setCharAt(int index,char a)替换字符;setLength()为重新设置字符串长度
// 从前往后替换,后面的字符要不断往后移动,要多次移动,所以效率低下
// 从后往前,先计算需要多少空间,然后从后往前移动,则每个字符只为移动一次,这样效率更高一点。
public class Solution {
public String replaceSpace(StringBuffer str) {
int spacenum = 0;//记录空格的数量
for (int i=0;i<str.length();i++ ) {
if(str.charAt(i)==' ')
spacenum++;
}
int indexold=str.length()-1;
int newLength = str.length()+2*spacenum;
int indexnew = newLength-1;
str.setLength(newLength);
for(;indexold>=0&&indexold<str.length();indexold--)
{
if(str.charAt(indexold)==' ')
{
str.setCharAt(indexnew--,'0');
str.setCharAt(indexnew--,'2');
str.setCharAt(indexnew--,'%');
}
else
{
str.setCharAt(indexnew--,str.charAt(indexold));
}
}
return str.toString();
}
}