Issue 5: Some interesting operations on strings


fifth period
PS: The solution to each question is not unique, welcome to discuss! After each question, there is an analysis to help you analyze and solve the question. The answer is at the bottom, and the bloggers will continue to update it every day.

1. Replace spaces

Title Description
Please implement a function to replace each space in the string s with "%20".
Example 1:
Input: s = "We are happy."
Output: "We%20are%20happy."
Example 2:
Input: s = "hello world."
Output: s = "hello%20world."
Parsing:
In Java We need to know that the string itself cannot be changed directly, so it can only be realized on a new string.
So we have an idea, create a new string, traverse the old string, add %20 to the new string when it is a space, add this character directly if it is not a space, and finally complete the traversal and get the character The string is the result we want. (Method 1)
Note:

  • To create a new string, you cannot create a String type. You need to create a StringBuilder, because Strings always create new strings when they are added, which consumes a lot of memory.
    -You can also use the library method replace(), but this will lose the meaning of brushing the questions, and during the interview, the interviewer does not want to see such code. (Method Two)

2. Left rotation of the string

Title description
The left rotation operation of a string is to transfer several characters in front of the string to the end of the string. Please define a function to realize the function of string left rotation operation. For example, if you input the string "abcdefg" and the number 2, the function will return the result "cdefgab" obtained by rotating left by two bits.
Example 1:
Input: s = "abcdefg", k = 2
Output: "cdefgab"
Example 2:
Input: s = "lrloseumgh", k = 6
Output: "umghlrlose"
Analysis:
There is a tricky way to this question, which is Apply the slice function to slice the string, calling the substring() library method. (method one).
This method may not be allowed during the interview, so we can use another method.
List traversal splicing, create a string of StringBuilder, sequentially concatenate k + 1 to the end character, then concatenate the first character to k, and finally return the string. (Method 2)
You can also do it by rotating characters, but it is more troublesome. We first convert the string into a character array, then rotate the 0 ~ k-1, k ~ arr.length - 1 coordinate elements, and finally rotate The entire array, and then convert this array into a string, we get the result we want. (Method 3)

3. Answer code

3.1 Replace spaces

	//方法一
    public String replaceSpace(String s) {
        StringBuilder stringbuilder = new StringBuilder();
        int len = s.length();
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            if(ch == ' '){
                stringbuilder.append("%20");
            }else{
                stringbuilder.append(ch);
            }
        }
        return stringbuilder.toString();
    }
    
    //方法二 
	public String replaceSpace(String s) {
    	return s.replace(" ", "%20");
	}

3.2 Left rotation of strings

	//方法一
    public String reverseLeftWords(String s, int n) {
        return s.substring(n) + s.substring(0, n); 
    }
    
    //方法二
    public String reverseLeftWords(String s, int n) {
        StringBuilder res = new StringBuilder();
        for(int i = n; i < s.length(); i++){
            res.append(s.charAt(i));
        }
        for(int i = 0; i < n; i++){
            res.append(s.charAt(i));
        }    
        return res.toString();
    }
       
    //方法三
    public String reverseLeftWords(String s, int n) {
        char[] arr = s.toCharArray();
        reverse(arr, 0, n - 1);
        reverse(arr, n, arr.length - 1);
        reverse(arr, 0, arr.length - 1);
        s = new String(arr);
        return s;
    }
    public void reverse(char[] arr, int left, int right){
        while(left < right){
            char tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

Guess you like

Origin blog.csdn.net/weixin_73392477/article/details/131110815