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 ...
随机推荐
- Flume源码-LoggerSink
package org.apache.flume.sink; import com.google.common.base.Strings; import org.apache.flume.Channe ...
- LANG=C是最早最简单的C语言环境(标准ASCII码)
LANG=C是最早最简单的C语言环境(标准ASCII码)
- oracle防火墙端口问题
Oracle服务端口方面会有很多的问题,下面就将为您介绍在防火墙上开放Oracle服务端口的方法,希望对您学习Oracle服务端口方面能有所帮助. 要使Oracle客户端能正常连接到设置有防火墙的安装 ...
- IBinder对象在进程间传递的形式(一)
命题 当service经常被远程调用时,我们经常常使用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind servic ...
- Android SimpleAdapter GridView (网格图片点击放大显示)
GridView网格视图 GridView网格视图是按照行,列分布的方式来显示多个组件,通常用于显示图片或是图标等,在使用网格视图时,首先需要要在屏幕上添加GridView组件. 常用属性: 1. a ...
- Node.js初体验
1.Node.js是什么 [1]Node是一个server端 JavaScript 解释器,但是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了.总结:水深不深我还不知道, ...
- [Javascript ] Array methods in depth - sort
Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...
- HDU 3791 二叉搜索树 题解
Problem Description 推断两序列是否为同一二叉搜索树序列 Input 開始一个数n,(1<=n<=20) 表示有n个须要推断,n= 0 的时候输入结束. 接下去一行是 ...
- [转] 深入剖析 linux GCC 4.4 的 STL string
本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...
- GCC编译选项
一.看例子分析gcc 的编译选项 gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld 1.-I /home/h ...