To prove safety offer5- string

topic

A string spaces replacing " % 20 " .

Thinking

 1. First, of course, is most likely to think of a new string, front to back, if you encounter a space, then add the add "% 20" in the new string; If you hit a normal character, it is stored in normal characters. (This is the case if the title allows you to create a new string)

2. (If the string is not allowed behind the new string, but requires to be replaced in the original string, but to ensure that input has enough free memory) then the easiest way is from front to back, hit the space character when replaced, because the replacement is a string of three characters, it is also necessary to first moved back behind the two strings, and then insert. But in this case, for each space, the trailing spaces are O (n) character is moved backward once, for strings containing O (n) space characters, the time complexity is O (n ^ 2).

3. The answers to such questions should be the best replacement back to front. Due to traverse the string again, you can count the number of spaces, resulting in an increase of the number of characters in the original string and then replaced. Providing two pointers, P1 points to the end of the original string, the end of the string, after the forward movement P1 and P2 point to copy characters to replace the pointer position P2, P2 is also moved forward so that, if the replacement space, then is added "20%" at P2, P2 pointer continues to move forward while (note reverse order) .

solution

    public static void replaceSpace(StringBuffer str) {
        int P1=str.length()-1;
        for(int i=0; i<=P1;i++) {
            if(str.charAt(i)==' ') {
                str.append("  ");
            }
        }
        int P2=str.length()-1;
        while(P1>=0 && P2>P1) {
            char c=str.charAt(P1--);
            if(c==' ') {
                str.setCharAt(P2--, '0');
                str.setCharAt(P2--, '2');
                str.setCharAt(P2--, '%');
            }
            else {
                str.setCharAt(P2--, c);
            }
        }
        System.out.print(str);
    }

 

 

topic 

There are two sorted array A1 and A2, A1 at the end of memory has enough free space to accommodate A2. Please write a function to insert all the numbers A2, A1, and all the numbers are sorted.

Thinking

Policy Implications on the question, this question should be from the back, because it is ordered array, then continue to the last digit of the comparison of the array, the greater number of its length to the last position of the array A1 (A1 is array num1 and num2 sum of the lengths) were added.

It should be noted that the two array length inconsistent, remember to process the remaining portion. When an array is not added to a new portion of the sequence is 0, indicating that the array has been done adding, then added directly to another portion of the array is not added.

solution

    public static void replaceSpace2(int[] num1, int[] num2, int length1, int length2) {
        //int len1=num1.length-1;
        //int len2=num2.length-1;
        int len1=length1-1;
        int len2=length2-1;
        int mergeLen=length1+length2-1;
        while(len1>=0 && len2>=0) {
            if(num1[len1]>num2[len2]) {
                num1[mergeLen]=num1 [len1] 
                len1 - ; 
                mergeLen - ; 
            } 
            Else  if (num1 [len1] < num2 [len2]) {             
                num1 [mergeLen] = num2 [len2] 
                len2 - ; 
                mergeLen - ; 
            } 
            Else { 
                num1 [mergeLen -] = num1 [len1-- ] 
                num1 [mergeLen -] = num2 [len2-- ] 
            } 
        } 
        While (len1> = 0 ) { 
            num1 [mergeLen--]=num1[len1--];
        }
        while(len2>=0) {
            num1[mergeLen--]=num2[len2--];
        }
        System.out.print(num1);
    }

 

Extended --StringBuffer class and StringBuilder class

String class can not be modified once created String object whose value can not be changed (constant pool contents in open space during the beginning of the assignment can not be modified). When the string modifications, and requires the use of StringBuffer class StringBuilder class , two kinds of objects that can be repeatedly modified, and no new unused objects.

The biggest difference between StringBuilder and StringBuffer StringBuilder method is not thread-safe (can not synchronize access). However, because there are StringBuffer StringBuilder compared to the speed advantage, so in most cases we recommend using the StringBuilder class . However, in the application case requires thread-safe, you must use the StringBuffer class .

Common methods:

// the StringBuffer 
public the StringBuffer () // constructor with no arguments 
public the StringBuffer ( int Capacity) // specified target string buffer capacity 
public the StringBuffer (String STR) // string buffer objects specified string content 
public INSERT ( int offset, int I) // the int representation of the argument string into this sequence 
public Delete ( int Start, int End) // remove this sequence of characters in the substring in the 
Replace ( int Start, int End, string STR) // given string substring of characters replacement of this sequence of characters 

//String class and method similar 
public  int Capacity () // Returns the current capacity, the theoretical value 
public  int length () // Returns the length (number of characters), the actual value of the 
char the charAt ( int index) // Returns the specified sequence index char value at 
int the indexOf (string STR) // returns substring index of the first occurrence of the string 
void the setLength ( int the newLength) // length character sequence provided 
string the substring ( int Start) returns a the new String, which contains the character subsequence of this sequence of characters currently contained in the
 void setCharAt ( int index, char CH) // character at a given index is set to CH 
String toString ()// Returns the string representation of the data sequence

Guess you like

Origin www.cnblogs.com/lyeeer/p/12194108.html