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 ...
随机推荐
- ssh端口映射,本地转发
应用场景: # HOSTA<-X->HOSTB 表示A,B两机器相互不可以访问, HOSTA<-->HOSTB 表示A,B两机器可以相互访问# 1.localhost< ...
- 我与Ubuntu的5年共成长
初次接触Ubuntu那还是2010年的初夏,那年大学二年级即将结束,为了增加计算机学院学生的技术能力和就业竞争力,学校组织了很多培训机构.公司企业来学校做技术宣讲.分享等 记得有一个企业是做Mp3 M ...
- uva 317 - Hexagon(规律推导)
题目连接:317 - Hexagon 题目大意:在一个19个六边形组成的图形上玩一个游戏,给出9个数字, 分成3组, 分别可以填在左上角, 上, 有上角,因为对于小六边形来说, 对边的数是相同的, 然 ...
- cocos2d-3.0 Helloworld::onTouchMoved的处理机制的推測
bool sign2 = true; bool sign2 = true; void GameLayer::onTouchMoved(Touch *touch, Event *unused){ if( ...
- HibernateProxy异常处理 java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
这里使用google的Gson包做JSON转换,因为较早的1.4版本的FieldAttributes类中没有getDeclaringClass()这个方法,这个方法是获取field所属的类,在我的排除 ...
- C#委托和事件本质
C#中委托和事件是很重要的组成部分,而掌握委托和事件的本质将必不可少.为了能探秘本质,写了如下代码 using System; using System.Collections.Generic; us ...
- memcache缓存命中深入理解转载
http://www.iteye.com/topic/225692 memcache的方法有 add,set,replace,get,delete,getstats,increment,decreme ...
- gitHub项目框架使用排名
项目名称 项目简介 使用心得 1. react-native 这 个是 Facebook 在 React ...
- CentOs上搭建git服务器
CentOs上搭建git服务器 首先安装setuptools wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0 ...
- [php基础]PHP.INI配置:Session配置详细说明教程
网上有很多PHP.INI文件配置的中文说明,但是对于PHP初学者来说在进行PHP运行环境搭建配置时还是容易一头雾水,今天换一种角度来分享如何进行php.ini配置,以求达到解决实际问题的效果,开篇以P ...