Sword refers to Java Implementation of offer programming questions -- interview question 4 replaces spaces, and sword refers to offer

Source: Internet
Author: User

Sword refers to Java Implementation of offer programming questions -- interview question 4 replaces spaces, and sword refers to offer
Implement a function to replace spaces in a string with "% 20 ". For example, if the string is We Are Happy, the replaced string is We % 20Are % 20 Happy.

1 package Solution; 2 3/** 4 * offoffoffer interview question 4: replace space 5 * Question: Please use a function to replace each space in the string with "% 20 ". 6 * For example, if "We are happy." Is input, "We % 20are % 20happy." Is output .". 7 * @ author GL 8*9 */10 public class No4ReplaceSpace {11 12 public static void main (String [] args) {13 String string1 = "We are happy. "; 14 String string2 =" We are happy. "; 15 String string3 =" Wearehappy. "; 16 // String string4 = null; 17 String string5 =" "; 18 String string6 =" "; 19 String string7 =" "; 20 System. out. println (replaceSpace (string1.toCharArray (); 21 System. out. println (replaceSpace (string2.toCh ArArray (); 22 System. out. println (replaceSpace (string3.toCharArray (); 23 // System. out. println (replace (string4.toCharArray (); 24 System. out. println (replaceSpace (string5.toCharArray (); 25 System. out. println (replaceSpace (string6.toCharArray (); 26 System. out. println (replaceSpace (string7.toCharArray (); 27 System. out. println (replaceSpace (new StringBuffer (string1); 28 System. out. println (replaceSpac E (new StringBuffer (string2); 29 System. out. println (replaceSpace (new StringBuffer (string3); 30 // System. out. println (replaceSpace (new StringBuffer (string4); 31 System. out. println (replaceSpace (new StringBuffer (string5); 32 System. out. println (replaceSpace (new StringBuffer (string6); 33 System. out. println (replaceSpace (new StringBuffer (string7); 34} 35 36/* 37 * time complexity is O (n) solution: use the array to replace 38*1 from the back and forward. traverse the array once. String to calculate the number of characters to replace 39*2. Create a temporary array, the array length is the number of characters in the initial string + the number of characters to replace each character * The number of characters to replace is 40*3. The original string array is copied forward and replaced with the temporary array.. The two array subscript variables are respectively the length of the original character array-1, the length of the temporary array-141*4, if the character of the original character array is not a space, copy this character to the corresponding position of the temporary array. Both arrays are marked with a minus of 142*5. If the character of the original character array is a space, then, replace the character corresponding to the temporary array from the back to the front, reduce the number of replacement characters in the temporary array subscript by 43*6, and replace by cyclic replication, after the subscript of the initial array is less than 0, the replacement is completed. After the temporary array is converted to a String, 44 */45 public static string replaceSpace (char [] String) is returned) {46 if (string = null) 47 return null; 48 int originalLength = string. length; 49 int spaceCount = 0; 50 for (int I = 0; I <originalLength; I ++) {51 if (string [I] = '') 52 spaceCount ++; 53} 54 int newLength = originalLength + 2 * spaceCount; 55 char [] temp = new char [newLength]; 56 int I = originalLength-1; 57 int j = newLength-1; 58 while (I> = 0) {59 if (string [I] = '') {60 temp [j] = '0 '; 61 temp [J-1] = '2'; 62 temp [J-2] = '%'; 63 j = J-3; 64} else {65 temp [j] = string [I]; 66 j = J-1; 67} 68 I = I-1; 69} 70 return new String (temp); 71} 72 73/* 74 * time complexity for O (n ), use StringBuffer to implement 75 * using the indexOf (String str, int fromIndex) method and subSequence (int start, int end) method to implement 76 */77 public static String replaceSpace (StringBuffer str) {78 if (str = null) 79 return null; 80 int fromIndex = 0; 81 int index = 0; 82 StringBuffer temp = new StringBuffer (); 83 while (index <= str. length () {84 index = str. indexOf ("", fromIndex); 85 if (index> = 0) {86 temp. append (str. subSequence (fromIndex, index )). append ("% 20"); 87 index = index + 1; 88 fromIndex = index; 89} else {90 temp. append (str. substring (fromIndex, str. length (); 91 break; 92} 93} 94 return temp. toString (); 95} 96 97}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.