Manually implement a string content replacement method to replace replace

foreword

replaceMethod and replaceAllmethod 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:

insert image description here
If you use replacethe 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:

insert image description here

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:
insert image description here

There is a character with a space in the string 13, and the space means charthat 32if you want to %20replace you must increase charthe 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:

insert image description here

So here are four lengths that need to be calculated:

  1. original string length
  2. the length of the replacement string
  3. the length of the replaced string
  4. 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 = 17the result 17is consistent with the length of the above picture

Code

Because the replacement string is known to be a space, so fixed- 1bit 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 1and judge the empty string condition 32with the length of the replaced string and char值you can use multiple strings and standard strings.

Guess you like

Origin blog.csdn.net/AnNanDu/article/details/126829679