剑指offer算法 java实现 替换字符串空格

来源:互联网 发布:vs 变量已被优化掉 编辑:程序博客网 时间:2024/06/05 09:10

剑指offer算法 java实现


面试题:替换空格(假设在原来的字符串上替换,并且保证输入的字符串后面有足够多的内存空间)

题目:请实现一个函数,把字符串中的每个空格替换成“20%”。例如输入“hellow new world!”,则输出“hellow02%new02%world!”。


原因:在网络编程中,如果URL参数中含有特殊字符,如:空格、“#”等,可能导致服务器端无法获得正确的参数值。我们需要将这些特殊符号转换成服务器识别的字符。转换规则是在“%”后面跟上ASCII码的两位十六进制的表。比如:空格的ASCII玛是32,即十六进制的0x20,因此空格被替换成“%20”。


时间复杂度为o(n2)不足以拿到offer

考虑时间复杂度为O(n)的解法,搞定offer就靠它了

算法描述如下:

从字符串的后面开始复制和替换,首先准备两个指针,p1和p2,p1指向原始字符串的末尾,p2指向替换后字符串的末尾,接下来,向前移动指针p1,逐个把它指向的字符复制到p2,碰到一个空格之后,把p1向前移动1格,在p2处插入字符串“20%”,由于“20%”长度为3,同时也要把p2向前移动3格。直到p1=p2,表明所有空格都已经替换完毕。


具体算法实现如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.offer.www;  
  2.   
  3. public class ReplaceBlank {  
  4.     private static String testString = "hellow new world!";  
  5.   
  6.     // 计算字符串中包含的空格个数  
  7.     public static int getBlankNum(String testString) {  
  8.         int count = 0;  
  9.         for (int i = 0; i < testString.length(); i++) {  
  10.             String tempString = String.valueOf(testString.charAt(i));  
  11.             if (tempString.equals(" ")) {  
  12.                 count++;  
  13.             }  
  14.         }  
  15.         return count;  
  16.     }  
  17.   
  18.     // 打印char[]数组  
  19.     public static void printArray(char[] testArray) {  
  20.         for (char i : testArray) {  
  21.             System.out.print(i);  
  22.         }  
  23.         System.out.println();  
  24.     }  
  25.   
  26.     // 将字符串空格转换为20%  
  27.     public static void replaceAllBlank(String testString) {  
  28.   
  29.         if (testString == null || testString.length() <= 0) {  
  30.             return;  
  31.         }  
  32.         // 字符数组初始长度  
  33.         int originalLength = testString.length();  
  34.         // 字符数组增加长度后  
  35.         int newLength = getBlankNum(testString) * 2  
  36.                 + originalLength;  
  37.         char[] tempArray = new char[newLength];  
  38.         System.arraycopy(testString.toCharArray(), 0, tempArray, 0, testString  
  39.                 .toCharArray().length);  
  40.         int indexofOriginal = originalLength - 1;  
  41.         int indexofNew = newLength - 1;  
  42.         System.out.println("未替换空格时的字符串:");  
  43.         printArray(tempArray);  
  44.         while (indexofOriginal >= 0 && indexofOriginal != indexofNew) {  
  45.             if (tempArray[indexofOriginal] == ' ') {  
  46.                 tempArray[indexofNew--] = '%';  
  47.                 tempArray[indexofNew--] = '2';  
  48.                 tempArray[indexofNew--] = '0';  
  49.             } else {  
  50.                 tempArray[indexofNew--] = tempArray[indexofOriginal];  
  51.             }  
  52.             indexofOriginal--;  
  53.         }  
  54.         System.out.println("替换空格后的字符串:");  
  55.         printArray(tempArray);  
  56.   
  57.     }  
  58.   
  59.     public static void main(String[] args) {  
  60.         replaceAllBlank(testString);  
  61.     }  
  62. }  



算法输出如下:

未替换空格时的字符串:
hellow new world!

替换空格后的字符串:
hellow02%new02%world!

1 0
原创粉丝点击