To prove safety Offer_ programming problem _ replace spaces

Title Description

Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.
 
 
 
Links: https://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423?answerType=1&f=discussion
Source: Cattle-off network

Ideas:

  1. Solution one: built-in Java function str.toString () replace ( "", "% 20").
  2. Solution two: on replacing the current string.
    1. Calculating the first replacement string require much space, expansion space and the original string;
    2. From the back, then the replacement string, each string move only once;
    3. If the front to back, each string needs to be moved many times less efficient.
  3. Solution three: open up a new string.

  4. for Four (I add): You can look back in the past, if you see a space, put behind a string of spaces are moved three lengths back. (Think of this problem with the way you can correct me in the comments below)

Code:

public class Test {
public static void main(String[] args) {
System.out.println( new Solution().replaceSpace(new StringBuffer("We Are Happy")));;
System.out.println( new Solution().replaceSpace2(new StringBuffer("We Are Happy")));;
}
}

  • This is the way to achieve two problem-solving ideas

First go over the original string, the length and the number of spaces in which to find out the string, depending on the number and length of the original string of spaces we can find the length of the last new string. Providing two pointers point to the end point1 point2 and location of the original string and the new string. If the content is not point1 point space, then assign the content to the location point2 points, if point1 point to a space, then the assignment from start point2 "02%" until point1 == point2 show when all spaces characters have been replaced finished .



class Solution {
//
// 链接:https://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423?answerType=1&f=discussion
// 来源:牛客网

public String replaceSpace(StringBuffer str) {
int num=0;
int spacenum = 0;
for(int i = 0; i < str.length(); i++){
num++;
if(str.charAt(i) == ' '){
spacenum++;
}
}
int oldLength = str.length();
int oldIndex = oldLength - 1;
int newLength = oldLength + spacenum*2;
str.setLength(newLength);
int newIndex = newLength - 1;
for (; oldIndex The> = 0 && oldLength <the newLength; oldIndex The -) {
NUM ++;
IF (str.charAt (oldIndex The) == '') {
str.setCharAt (newIndex--, '0');
str.setCharAt ( newIndex--, '2');
str.setCharAt (newIndex--, '%');
} the else {
str.setCharAt (newIndex--, str.charAt (oldIndex The));
}
}
System.out.println ( " the number of "+ NUM);
return str.toString ();
}


// for four (I add): You can look back in the past, if you see a space, put behind a string of spaces are moved backward 3 length. (Think of this problem with the way you can correct me in the comments below)


public String replaceSpace2 (StringBuffer str) {
int NUM = 0;
int has = 0;
for (int I = 0; I <str.length () - has; I ++) {
IF (str.charAt (I) == '') {
// spaces, not cycle the three described later
has = 3;
str.length len = int ();
int len = + NewL. 3;
str.setLength (NewL);
for (int J = NewL; J> I +. 3; J,) {
str.setCharAt (-J. 1, STR .charAt (len--));
NUM ++;
}
str.setCharAt (I ++, '%');
str.setCharAt (I ++, '2');
str.setCharAt (I ++, '0');

} the else {
NUM ++;
}

}
System.out.println("次数 "+num);
return str.toString();
}

}

result:


The above two methods are cycling 24 times

The number of 24-
We% 20Are% 20Happy
the number of 24-
We% 20Are% 20Happy


Reference: https://blog.csdn.net/cpongo1/article/details/88536263

Guess you like

Origin www.cnblogs.com/liran123/p/12634828.html