zoukankan      html  css  js  c++  java
  • 替换空格

       请实现一个函数,把字符串中每个空格替换成“20%”,例如:输入“we are happy.”,则输出“we20%are20%happy.”。

    如果我们最直观的做法从头到尾扫描字符串,每一次碰到空格的时候做替换。假设字符串长度是n,对每个空格字符,需要移动后面的O(n)个字符,因此对含有O(n)个空格字符的字符串而言总的时间效率是O(n^2)。在这里如果用java的替换方法replace可以轻松搞定

    时间复杂度O(n^2)的代码

    1 package com.feimao.com.feimao.a2.test;
    2 
    3 public class ReplaceBlank {
    4     static final String s = "We are happy.";
    5     public static void main(String[] args){
    6         System.out.println("原数组 = " + s);
    7         System.out.println("替换的数组 " + s.toString().replace(" " , "20%"));
    8     }
    9 }

      我们换一种思路,把从前向后替换改成从后向前替换。

      我们可以先遍历一次字符串,这样就能统计出字符串的空格的总数。变可以由此计算替换之后字符串的总长度。每替换一个空格,长度增加2,因此替换后的字符串的长度等于原来长度加上2*空格数。

    统计字符串的空格总数代码

     1     public static int getBlankNum(String s) {
     2         int count = 0;
     3         for (int i = 0; i < s.length(); i++) {
     4             String tempS = String.valueOf(s.charAt(i));
     5             if (tempS.equals(" ")) {
     6                 count++;
     7             }
     8         }
     9         return count;
    10     }

      首先准备两个指针,p1和p2。p1指向原始字符串的末尾,p2指向替换之后字符串的末尾。接下来我们向前移动指针p1,逐个把它指向字符复制到p2指向的位置,直到碰到第一个空格为止,也就是做了字符串拷贝。整个算法代码如下:

     1 package com.feimao.com.feimao.a2.test;
     2 
     3 public class ReplaceBlank {
     4     public static String s = "We are happy.";
     5     public static int getBlankNum(String s) {
     6         int count = 0;
     7         for (int i = 0; i < s.length(); i++) {
     8             String tempS = String.valueOf(s.charAt(i));
     9             if (tempS.equals(" ")) {
    10                 count++;
    11             }
    12         }
    13         return count;
    14     }
    15 
    16     public static void printArray(char[] testArray) {
    17         for (char i : testArray) {
    18             System.out.print(i);
    19         }
    20         System.out.println();
    21     }
    22 
    23     public static void replaceBlank(String s) {
    24         if (s == null || s.length() < 0) {
    25             System.out.println("请正确输入数组");
    26             return;
    27         }
    28         int length = s.length();
    29         int newLength = s.length() + getBlankNum(s) * 2;
    30         char[] temparray = new char[newLength];
    31         System.arraycopy(s.toCharArray(), 0, temparray, 0, s.toCharArray().length);
    32         int p1 = length - 1;
    33         int p2 = newLength - 1;
    34         while (p1 >= 0 && p1 != p2) {
    35             if (temparray[p1] == ' ') {
    36                 temparray[p2] = '0';
    37                 p2--;
    38                 temparray[p2] = '2';
    39                 p2--;
    40                 temparray[p2] = '%';
    41                 p2--;
    42             } else {
    43                 temparray[p2] = temparray[p1];
    44                 p2--;
    45             }
    46             p1--;
    47         }
    48         System.out.println("替换空格后的字符串: ");
    49         printArray(temparray);
    50     }
    51 
    52     public static void main(String[] args) {
    53         System.out.println("原字符串 = " + s);
    54         replaceBlank(s);
    55     }
    56 
    57 
    58 }

    运行结果:

  • 相关阅读:
    php服务器安装memcache
    [PHP脚本]安装及使用
    [PHP]针对外服务器mail函数的php.ini配置
    [COBOL]安装配置及大型机模拟Hercules配置
    [Sqlite3].help中的命令介绍
    [NavigatLite4Mysql]DB管理工具使用
    [MS-SQLserver2005]Windows64位安装问题
    [MS-SQL]20130806_LocalDB_DOS命令使用
    [MS-SQL]20130806_LocalDB安装
    [MS]Microsoft SQL Server 2008 R2 开发版/企业版/标准版
  • 原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/10193438.html
Copyright © 2011-2022 走看看