-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
47 lines (43 loc) · 1.6 KB
/
Solution.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
package ds.pointer.targetoffer05;
import java.util.Arrays;
/**
* 替换空格
* 剑指offer 05 https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
*
* @author yangyi 2021年01月19日17:21:34
*/
public class Solution {
public String replaceSpace(String s) {
int spaceCount = 0;
char[] chars = s.toCharArray();
for (char c : chars) {
if (c == ' ') {
spaceCount++;
}
}
char[] newChars = Arrays.copyOf(chars, chars.length + spaceCount * 2);
for (int oldIndex = chars.length - 1, newIndex = newChars.length - 1; oldIndex < newIndex; oldIndex--, newIndex--) {
if (newChars[oldIndex] != ' ') {
newChars[newIndex] = newChars[oldIndex];
} else {
newChars[newIndex--] = '0';
newChars[newIndex--] = '2';
newChars[newIndex] = '%';
}
}
return new String(newChars);
}
public static void main(String[] args) {
String string = "we are Chinese";
String string1 = " ";
String string2 = "";
String string3 = " ";
String string4 = " we are Chinese ";
Solution replaceBlankInString = new Solution();
System.out.println(replaceBlankInString.replaceSpace(string));
System.out.println(replaceBlankInString.replaceSpace(string1));
System.out.println(replaceBlankInString.replaceSpace(string2));
System.out.println(replaceBlankInString.replaceSpace(string3));
System.out.println(replaceBlankInString.replaceSpace(string4));
}
}