Sword Finger Offer 05. Five ways to replace spaces

Jian Zhi Offer 05. Replace spaces

Please implement a function to replace each space in the string s with "%20".

Insert picture description here
Method 1: Use StingBuffer

    public static String replaceSpace(String s) {
    
    
        //new StringBulider(速度快,线程不安全)或者StringBuffer填充
        StringBuilder sb = new StringBuilder();
        char[] arry = s.toCharArray();
        for (int j = 0; j < arry.length; j++) {
    
    
            if (arry[j] == ' ') {
    
    
                sb.append("%20");
            } else sb.append(arry[j]);
        }
        // 同String.valueOf(sb);
        return sb.toString();
    }

1. Convert String type data to char type array,
2. Use StingBuffer to create a new modifiable array
3. Traverse the array one by one, insert characters that are not spaces directly, and insert characters that are spaces with %20
4. Finally Convert data to String type

Method 2: Use the API directly

    public static String replaceSpaceTwo(String s) {
    
    
        String value = s.replaceAll(" ","%20");
        return value;
    }

Using replace should convert a single character, but changing replaceAll to replace can also perform the same result.
Insert picture description here
Method 3: Insert in reverse order
1. First traverse the string and record the number of spaces, and create a character with length + number of spaces*2 Array
2. Traverse the string from the back
3. If the character is a non-space, insert it as the original character, pay attention to the number of the insertion position -1
3. If the character is a space, insert %20 one by one, and each character -1

    public static  String replaceSpaceThree(String s){
    
    
        int count = 0;
        int oldlen = s.length() - 1;
        // 旧数组长度-1,表示最后一个数的下标
        char[] sArry = s.toCharArray();
        for(int i =0; i < s.length(); i++){
    
    
            if(sArry[i] == ' '){
    
    
                count ++;
            }
        }
        char[] value = new char[(s.length() + count *2)];
        // 新的数组长度-1,表示最后一个数的下标
        int newlen = value.length-1;
        for(int j  = oldlen; j >= 0;j--){
    
    
            if(sArry[j] == ' '){
    
    
                value[newlen--] = '0';
                value[newlen--] = '2';
                value[newlen--] = '%';
            }
            else{
    
    
          		//--是先赋值后再-1
                value[newlen--] = sArry[j];
            }
        }
        return String.valueOf(value);
    }

Note: Insert in reverse order, so %20 of the insertion position must become 02%

The idea is not difficult. The blogger's first idea was to insert in a later order, and later thought that the length of the new array was determined, why not insert it in a positive order? It turns out that as long as two variables are used to control the subscript when inserting the array, it can be achieved. The blogger first used a subscript to control the new array subscripts +1 and +2, but it is difficult to determine the subscript assigned to j when there is no space in the subsequent sequence! Not much to say here!

Method 4: Positive sequence insertion

    public static  String replaceSpaceFour(String s){
    
    
        int count = 0;
        int oldlen = s.length() - 1;
        // 旧数组长度-1,表示最后一个数的下标
        char[] sArry = s.toCharArray();
        for(int i =0; i < s.length(); i++){
    
    
            if(sArry[i] == ' '){
    
    
                count ++;
            }
        }
        char[] value = new char[(s.length() + count *2)];
        // 新的数组下标从0开始
        int newlen = 0;
        for(int j  = 0; j <= oldlen;j++){
    
    
            if(sArry[j] == ' '){
    
    
                value[newlen++] = '%';
                value[newlen++] = '2';
                value[newlen++] = '0';
            }
            else{
    
    
                //--是先赋值后再-1
                value[newlen++] = sArry[j];
            }
        }
        return String.valueOf(value);
    }

The organization of these two methods wastes a lot of my time, because the official Jianzhi offer is the following method. First, a space of 3 times the length is opened up, and then inserted one by one, and finally the first size bit used is output , I personally feel that a lot of space was wasted during the first development, so I have been thinking about the above writing, it is an example that can be learned!

Method 5: Sword refers to offer

class Solution {
    
    
    public String replaceSpace(String s) {
    
    
        int length = s.length();
        char[] array = new char[length * 3];
        int size = 0;
        for (int i = 0; i < length; i++) {
    
    
            char c = s.charAt(i);
            if (c == ' ') {
    
    
                array[size++] = '%';
                array[size++] = '2';
                array[size++] = '0';
            } else {
    
    
                array[size++] = c;
            }
        }
        String newStr = new String(array, 0, size);
        return newStr;
    }
}

In fact, observing the whole process, there is no difference, just the difference in the length of the array at the beginning!

Guess you like

Origin blog.csdn.net/weixin_46801232/article/details/108692915