【54】Replace spaces | Print linked list from end to beginning

Replace spaces

Problem Description

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

Problem-solving ideas

Traverse the string, if a space is found, make the new string equal to the paragraph before the space + "%20" + the paragraph after the space.

class Solution {
    
    
    public String replaceSpace(String s) {
    
    
        for(int i=0; i<s.length(); i++){
    
    
            if(s.charAt(i) == ' '){
    
    
                s = s.substring(0,i) + "%20" + s.substring(i+1);
                i+=2; //注意因 %20 而导致的序号增大
            }
        }
        return s;
    }
}

Time complexity: O(n 2 ) The time complexity of the substring() method is O(n)
Space complexity: O(1)

A way to change space for time:

class Solution {
    
    
    public String replaceSpace(String s) {
    
    
        String res = "";
        int n =s.length();
        for(int i=0; i<n; i++){
    
    
            char c = s.charAt(i);
            switch(c){
    
    
                case ' ' :
                    res += "%20";
                    break;
                default:
                    res += c;
                    break;
            }
        }
        return res;
    }
}

Time complexity: O(n)
Space complexity: O(n + 2k) (n is the length of the original string, k is the number of spaces)

The solution provides the idea of ​​double pointers, first count the number of spaces, and then add the required space in the string s in advance, then one pointer points to the last position of the new string, and the other pointer points to the original string For the last element, fill with "%20" if the element is a space, otherwise, make the elements pointed to by the two pointers equal. When the two pointers point to the same position, jump out of the loop.

Small summary

Usage of substring() method:

  1. substring(int beginIndex) form
    This method is used to extract the part of the string from the index position to the end . When calling, the starting position of the string to be extracted is in the parentheses, and the return value of the method is the extracted string.
  1. substring (int beginIndex, int endIndex) form
    in this method indicates the start index beginIndex taken, including the character string taken in corresponding to the starting index; endIndex index indicates the end, the string is not included in the end taken corresponding to the index character. This method is used to extract the string part between the position beginIndex and the position endIndex .

note:

For the start position beginIndex, Java processes the first character index of the string as 0, but for the end position endIndex, Java processes the first character index of the string as 1.

Print linked list from end to beginning

Problem Description

Enter the head node of a linked list, and return the value of each node from the end to the beginning (return with an array).

Problem-solving ideas

First find out the length of the linked list, apply for an appropriate memory size for the array that stores the result, traverse the linked list again, and store its values ​​in the array from back to front.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public int[] reversePrint(ListNode head) {
    
    
        int n = getListLength(head);
        int res[] = new int[n];
        for(int i=n-1;i>=0;i--){
    
    
            res[i] = head.val;
            head = head.next;
        }
        return res;
    }

    public int getListLength(ListNode head){
    
    
        int n = 0;
        while(head != null){
    
    
            n++;
            head = head.next;
        }
        return n;
    }
}

Time complexity: O(n)
Space complexity: O(n)

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/114871878