foreword
replace
Method and replaceAll
method are the most commonly used solutions in the case of string replacement , so what would you do if you were asked to manually handle string replacement?
《剑指Offer》
The 05题
one is this:
If you use replace
the method one line of code to process
class Solution {
public String replaceSpace(String s) {
return s.replace(" ","%20");
}
}
If I were to implement it myself, 代码如下
:
class Solution {
public String replaceSpace(String s) {
byte[] cs = s.getBytes();
byte[] addByte = "%20".getBytes();
int addIndex = 0;
for (int i = 0; i < cs.length; i++) {
if (cs[i] == 32){
addIndex = addIndex + (addByte.length-1);
}
}
char[] c = new char[cs.length+addIndex];
int f = 0;
for (int i = 0; i < c.length; i++) {
if (cs[f] == 32){
for (int j = 0; j < addByte.length; j++) {
c[i+j] = (char) addByte[j];
}
i = i+(addByte.length-1);
}else{
c[i] = (char) cs[f];
}
f++;
}
return new String(c);
}
}
Results of the:
analyze
The code above is not much optimized in terms of complexity. It is just how to realize it in the initial consideration. Now let’s analyze the idea of replacing strings.
First of all, a string is composed of a character array, that is char[]
, taking a string “We are happy.”
as an example, the structure is as follows:
There is a character with a space in the string 13
, and the space means char
that 32
if you want to %20
replace you must increase char
the length of the array. If one character becomes three characters, you need to char数组
add two characters to the original length. Two spaces are four , the structure becomes as follows:
So here are four lengths that need to be calculated:
- original string length
- the length of the replacement string
- the length of the replaced string
- the length of the new string
The first three can be obtained directly, and the fourth needs to be calculated. The calculation formula is:
原始字符串长度 + (被替换字符串长度 - 替换字符串长度) * 替换字符串数量
Calculate the formula with the above string: 13 + (3 - 1) * 2 = 17
the result 17
is consistent with the length of the above picture
Code
Because the replacement string is known to be a space, so fixed- 1
bit characters, three of the above four lengths can be obtained, and the number of digits of the replaced character also needs to be calculated once.
code show as below:
public String replaceSpace(String s) {
// 获取原始字符串长度
byte[] sb = s.getBytes();
// 获取替换字符串长度
byte[] addByte = "%20".getBytes();
// 计算被替换字符串出现次数
int addIndex = 0;
for (int i = 0; i < sb.length; i++) {
if (sb[i] == 32){
addIndex++;
}
}
// 计算新字符串长度
char[] c = new char[sb.length+(addByte.length-1)*addIndex];
}
After these four values are obtained, it is to fill the content of the new string array, which requires a count 原始字符串
and 遍历位数
a record 新字符串
. 遍历位数
If it is + 新字符串长度>旧字符串长度
when encountering a replacement string 新字符串
, otherwise , it is + when encountering a replacement string .遍历位数
(被替换字符串长度 - 替换字符串长度)
新字符串长度<旧字符串长度
旧字符串
遍历位数
(被替换字符串长度 - 替换字符串长度)
Here %20 > ''
are the traversal digits of the new string + (被替换字符串长度 - 替换字符串长度)
, the code is as follows:
public String replaceSpace(String s) {
// 获取原始字符串长度
byte[] sb = s.getBytes();
// 获取替换字符串长度
byte[] addByte = "%20".getBytes();
// 计算被替换字符串出现次数
int addIndex = 0;
for (int i = 0; i < sb.length; i++) {
if (sb[i] == 32){
addIndex++;
}
}
// 计算新字符串长度
char[] c = new char[sb.length+(addByte.length-1)*addIndex];
// 记录旧字符串遍历位数
int f = 0;
// 循环新字符串
for (int i = 0; i < c.length; i++) {
if (sb[f] == 32){
for (int j = 0; j < addByte.length; j++) {
c[i+j] = (char) addByte[j];
}
// 遇到替换字符串新字符遍历位数跳过(被替换字符串长度 - 替换字符串长度)
i = i+(addByte.length-1);
}else{
c[i] = (char) sb[f];
}
f++;
}
return new String(c);
}
According to the above formula , replace 1
and judge the empty string condition 32
with the length of the replaced string and char值
you can use multiple strings and standard strings.