[offer-02] Replace the blanks
- Test Point: String
- Time limit: 1 second
- Space limitation: 32768K
- Implement a function that replaces each space in a string with "% 20". For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.
Ideas:
- I have an idea right away, because it's given StringBuffer type, so I convert it into String type by toString function. String type has its own method replaceAll. The first parameter is the character to be replaced, and the second parameter is the character to be replaced. Make a sentence.
Code:
public class Solution { public String replaceSpace(StringBuffer str) { return str.toString().replaceAll(" ", "%20"); } }
My question:
- The original intention of this question is to write a method to replace String instead of calling the original method of String. So we must open up a new way of thinking, there is another way to solve it.
Other ideas:
- Question 1: Replacing strings is to replace the original strings or to create a new string to replace them!
- Question 2: In the current string substitution, how to replace is more efficient (regardless of the existing replacement method in java).
- It is inefficient to replace the characters from the beginning to the end and move them back and forth repeatedly.
- From back to front, first calculate how much space is needed, then move back to front, then each character moves only once, which is more efficient. ]
- First count the number of spaces.
- Then the new length = the old length + the number of spaces*2
- Expand the array length.
- Traverse backwards and forwards, setting the characters of each str subscript.
- Note here that indexOld is traversed one by one, starting with the old subscript. Start copying backwards. If it's a space, copy 02% backwards. If it's not a space, copy the original character.
public class Solution { public String replaceSpace(StringBuffer str) { int spacenum = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { spacenum++; } } int indexOld = str.length() - 1; int newLength = str.length() + spacenum * 2; str.setLength(newLength); int indexNew = newLength - 1; for ( ; indexOld >= 0 && indexOld < newLength; indexOld--) { if (str.charAt(indexOld) == ' ') { str.setCharAt(indexNew--, '0'); str.setCharAt(indexNew--, '2'); str.setCharAt(indexNew--, '%'); } else { str.setCharAt(indexNew--, str.charAt(indexOld)); } } return str.toString(); } }
The function to set the length is setLength, which is used for StringBuffer.
The function to set a character is: setCharAt(index, i)
Other ideas 2:
- Another relatively simple method is to create a new string, which requires only one iteration.
- However, it requires more space complexity, and O(n) stores new strings.
public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer s = new StringBuffer(); for (int i = 0; i < str.toString().length(); i++) { char c = str.charAt(i); if (c == ' ') { s.append("%20"); } else { s.append(c); } } return s.toString(); } }
Attention! The length of string is obtained by means of length().
But the length of an array can be obtained directly by attributes, that is, length.
Other ideas 3:
- Record the number of''from front to back and replace''from back to front'.
- Focus: Skills in replacing from the back to the front
- For example, "we are lucky"
0 1 2 3 4 5 6 7 8 9 10 11
w e a r e l u c k y
You can know the number of counts = 2; // spaces.
So when replacing 711, the letters move backwards by count * 2 positions, and the letters of 35 move backwards by count-1 * 2 positions. So we get:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
w e a r e l u c k y
w e a r a r e u c k l u c k y
Write% 20 directly in the space when replacing.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
w e a r e l u c k y
w e % 2 0 a r e % 2 0 l u c k y
public class Solution { public String replaceSpace(StringBuffer str) { int spacenum = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { spacenum++; } } StringBuffer s = new StringBuffer(); s.setLength(str.length() + spacenum * 2); for (int i = str.length() - 1; i >= 0; i--) { if (str.charAt(i) != ' ') { s.setCharAt(i + 2 * spacenum, str.charAt(i)); } else { spacenum--; s.setCharAt(i + spacenum * 2, '%'); s.setCharAt(i + spacenum * 2 + 1, '2'); s.setCharAt(i + spacenum * 2 + 2, '0'); } } return s.toString(); } }
Attention! The length of string is obtained by means of length().
But the length of an array can be obtained directly by attributes, that is, length.