P44、面试题4:替换空格
题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。 |
如果用java string类中提供的replace方法可以很快的进行替换。
代码实现:
- package com.yyq;
- /**
- * Created by Administrator on 2015/9/4.
- */
- public class ReplaceBlank {
- public static void main(String args[]){
- String str = "we are happy";
- String newStr = str.replace(" ","%20");
- System.out.println(newStr);
- }
- }
如果是按照很原始的方法进行替换的话,里面包含着大技巧。
我们可以先遍历一次字符串,这样就能统计出字符串中空格的总数,并可以由此计算出替换之后的字符串的总长度。每替换一个空格,长度增加2,因此替换以后字符串的长度等于原来长度加上2乘以空格数目。从字符串的后面开始复制和替换,直到都走到字符串首位。
代码实现:
- package com.yyq;
- import java.util.Arrays;
- /**
- * Created by Administrator on 2015/9/4.
- */
- public class ReplaceBlank {
- private static int maxLength = 100;
- //length 为字符数组string的总容量,新增长的数组长度不能超过length
- public static void repalceBlank(String str, int trueLength, int maxLength) {
- if (str == null || maxLength <= 0) {
- return;
- }
- //如果是传统的静态数组,java中是不允许动态扩充的,如果需要扩充的话,则需要使用java自带的累积框架,如List
- char oldString[] = str.toCharArray();
- int numberBlank = 0;
- int i = 0;
- while (i < trueLength) {
- if (oldString[i] == ' ') {
- numberBlank++;
- }
- i++;
- }
- int newLength = trueLength + numberBlank * 2;
- //新开辟一个数组
- char newString[] = new char[newLength];
- for (int j = 0; j < newLength; j++) {
- newString[j] = 0;
- }
- if (newLength > maxLength) {
- return;
- }
- int indexOld = trueLength - 1;
- int indexNew = newLength - 1;
- while (indexOld >= 0 && indexOld <= indexNew) {
- if (oldString[indexOld] == ' ') {
- newString[indexNew] = '0';
- indexNew--;
- newString[indexNew] = '2';
- indexNew--;
- newString[indexNew] = '%';
- indexNew--;
- } else {
- newString[indexNew--] = oldString[indexOld];
- }
- indexOld--;
- }
- System.out.println(new String(newString));
- }
- public static void Test(String testName, String str) {
- if (str == null) return;
- if (testName != null) {
- System.out.println(testName + "======");
- repalceBlank(str, str.length(), maxLength);
- }
- }
- // 空格在句子中间
- public void Test1()
- {
- String str = "ab c";
- Test("Test1(空格在句子中间)", str);
- }
- // 空格在句子开头
- public void Test2()
- {
- String str = " helloworld";
- Test("Test2(空格在句子开头)", str);
- }
- // 空格在句子末尾
- public void Test3()
- {
- String str = "helloworld ";
- Test("Test3(空格在句子末尾)", str);
- }
- // 连续有两个空格
- public void Test4()
- {
- String str = "hello world";
- Test("Test4(连续有两个空格)", str);
- }
- // 传入NULL
- public void Test5()
- {
- Test("Test5(传入null)", null);
- }
- // 传入内容为空的字符串
- public void Test6()
- {
- String str = "";
- Test("Test6传入内容为空的字符串", str);
- }
- //传入内容为一个空格的字符串
- public void Test7()
- {
- String str = " ";
- Test("Test7(传入内容为一个空格的字符串)", str);
- }
- // 传入的字符串没有空格
- public void Test8()
- {
- String str = "helloworld";
- Test("Test8(传入的字符串没有空格)", str);
- }
- // 传入的字符串全是空格
- public void Test9()
- {
- String str = " ";
- Test("Test9(传入的字符串全是空格)", str);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ReplaceBlank test = new ReplaceBlank();
- test.Test1();
- test.Test2();
- test.Test3();
- test.Test4();
- test.Test5();
- test.Test6();
- test.Test7();
- test.Test8();
- test.Test9();
- }
- }
输出结果:
Test1(空格在句子中间)====== ab%20c Test2(空格在句子开头)====== %20helloworld Test3(空格在句子末尾)====== helloworld%20 Test4(连续有两个空格)====== hello%20%20world Test6传入内容为空的字符串====== Test7(传入内容为一个空格的字符串)====== %20 Test8(传入的字符串没有空格)====== helloworld Test9(传入的字符串全是空格)====== %20%20%20 |
P44、面试题4:替换空格的更多相关文章
- 【剑指offer】面试题 5. 替换空格
面试题 5. 替换空格 题目:请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy. 则经过替换之后的字符串为We%20Are%20Hap ...
- 剑指Offer:面试题4——替换空格(java实现)
问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...
- C++版 - 剑指offer 面试题4: 替换空格 题解
面试题4:替换空格 提交网址: http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=1 ...
- 剑指offer编程题Java实现——面试题4替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package Solution; ...
- 剑指offer-面试题4.替换空格
题目:请实现一个函数,把字符串中的每个空格都替换成"%20".例如输入"We are happy." 则输出"We%20are%20happy.&qu ...
- 剑指offer面试题4 替换空格(java)
注:利用java中stringBuilder,append,length方法很方便的解决字符串问题 /* * 剑指offer 替换空格 * xsf * */ /*开始替换空格的函数,length为原数 ...
- 剑指offer面试题4 替换空格(c)
- 面试题04_替换空格_剑指Offer系列
题目描写叙述 请实现一个函数,将一个字符串中的空格替换成"%20". 比如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路 ...
- 《剑指offer》面试题4 替换空格 Java版
(给一个足够长的字符数组,其中有一段字符,将' '(空格)替换成'%' '2' '0'三个字符,原字符段由'\0'结尾) 书中方法:这道题如果从头到尾扫描数组并替换,会涉及到数组的移动.如果不移动元素 ...
- 【剑指Offer】面试题05.替换空格
题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...
随机推荐
- Z-Stack ZMain学习
[注:本文源自博客园http://www.cnblogs.com/cherishui/,为尊重劳动者成果,如需转载请保留此行] 在TI已有的Z-Stack的工程下面,打开已有的demo文件,通过分析不 ...
- L005-oldboy-mysql-dba-lesson05
L005-oldboy-mysql-dba-lesson05 在线改表工具:pt-onine-schema-change 来自为知笔记(Wiz)
- pdf转chm的实现方法
相比pdf, CHM电子书在Windows系统下不需要安装额外的浏览器即可进行阅读,其内容是基于浏览器的风格,更容易被用户所接受.而且, 具有更强大的功能配置,比如可提供强大的全文搜索.索引.书签等的 ...
- 选中excel中的对象
2007在查找和选择中点击“选择对象”,然后再全选全个sheet(ctrl+a)就可以看到了. 2010 “选择对象”在 开始——查找和选择——选择对象
- GridView ItemCommand
GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...
- 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample
application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdb ...
- Entity Framework 学习笔记(一)安装
1.通过 VS2013 下载,这个没有限制,因为我用的vs是2013 Entity Framework包
- 微软职位内部推荐-Senior NLP Scientist & Developer
微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...
- custom activities
Useful Sharepoint Designer Custom Workflow Activities http://spdactivities.codeplex.com/ http://stac ...
- linq and rest api in sharepoint
//1.make sure your application using the .net fromwork 3.5 //2.create entity classes using the instr ...