Cracking the coding interview--Q1.4
原文
Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional
characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation
in place.)
EXAMPLE
Input: "Mr John Smith "
Output: "Mr%20Dohn%20Smith"
译文:
写一个函数,把字符串中所有的空格替换为%20 ,并且原字符串末尾的空格要忽略。
解答
直接用了java.lang.String里的replaceAll方法,但是为了忽略末尾的空格,先做一些处理,将末尾的空格先修改成一个不可见的其他字符,ASCII小于20的都行。然后替换之后就用trim方法就能去除掉最后的不可见字符。
public class Main {
public static String replaceSpaces (String str) {
int len = str.length();
char a[] = str.toCharArray();
for(int i = len - 1; i >= 0; i--) {
if(a[i] == ' '){
a[i] = 0;
}
else
break;
}
String str2 = new String(a);
return str2.replaceAll(" ", "%20").trim();
}
public static void main(String args[]) {
String s1 = "Mr John Smith ";
System.out.println(replaceSpaces(s1));
}
}
但是如果原字符串首包含ASCII小于20的不可见字符的话就有BUG了。所以又改进了一个版本:
public class Main {
public static String replaceSpaces (String str) {
int len = str.length();
boolean flag = true;
StringBuilder sb = new StringBuilder();
for(int i = len - 1; i >= 0; i--) {
if(str.charAt(i) == ' ' && flag){
continue;
}
else if(str.charAt(i) == ' ' && !flag) {
sb.insert(0, "%20");
}
else {
sb.insert(0, str.charAt(i));
flag = false;
}
}
return sb.toString();
}
public static void main(String args[]) {
String s1 = "Mr John Smith ";
System.out.println(replaceSpaces(s1));
}
}
Cracking the coding interview--Q1.4的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 关于在SLES11, RHEL6, OEL6 and UEK2 Kernels使用hugepages的告警
ALERT: Disable Transparent HugePages on SLES11, RHEL6, OEL6 and UEK2 Kernels (Doc ID 1557478.1) Modi ...
- DecimalFormat用法
DecimalFormat用法 DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字. DecimalFormat 包含一个模式 和一组符号 符号含义: ...
- StarUML中时序图添加小人
转载于 http://blog.csdn.net/longyuhome/article/details/9011629 在看时序图的例子的时候,发现有些的时序图上有小人的图标,可是一些UML工具却没有 ...
- Android(java)学习笔记250:ContentProvider使用之获得系统联系人信息02(掌握)
1.重要: 系统删除一个联系人,默认情况下并不是把这个联系人直接删除掉了,只是做了一个标记,标记为被删除. 2.前面一讲说过了如何获取系统联系人信息(通过ContentProvider),获取联系人信 ...
- oracle手动删除数据库
有时候,无法使用图形界面时,我们需要手动删除数据库,具体操作步骤如下:一.手动删除文件系统数据库 1.停止监听,防止有新的连接产生,同时,在数据库配置了em的,也需要停止 $ lsnrctl st ...
- 配置HP LaserJet M1536dnf MFP打印机通过TCPIP共享多台计算机
HP LaserJet M1536dnf MFP打印机通过TCPIP共享多台计算机配置配置过程:1.确保网线插入到打印机后,点设置按钮,到网络配置中查看IP地址,确保与主机或者路由器的IP地址一致,设 ...
- 公共Webservice
网络上可供测试的Web Service腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService. ...
- Java多线程——多线程的基本概念和使用
一.进程和线程的基础知识 1.进程和线程的概念 进程:运行中的应用程序称为进程,拥有系统资源(cpu.内存) 线程:进程中的一段代码,一个进程中可以有多段代码.本身不拥有资源(共享所在进程的资源) 在 ...
- C# 面向对象 , 继承
继承 class A { Console.WriteLine("hello world"); } class B:A { } 以上书写,表示B 是A 的子类, B 同时继承A 中 ...
- sping注解原理
持续更新中.. spring注解用的是java注解,用到的是java反射机制. 参考文档如下: http://zxf-noimp.iteye.com/blog/1071765 对应spring源码如下 ...