2014-03-18 01:36

题目:给定一个字符串,将其中的空格‘ ’替换为‘%20’,你可以认为字符串尾部有足够空间来容纳新增字符。请不要额外开辟数组完成。

解法:先从前往后统计空格个数,然后从后往前填充字符,以免其他无关字符被‘%20’覆盖掉。

代码:

 // 1.4 Write a method to replace all spaces in a string with '%20'.
// do it in-place and backward.
#include <cstdio>
#include <cstring>
using namespace std; class Solution {
public:
void replaceSpace(char *str) {
if (nullptr == str) {
return;
} int i, j;
int len = (int)strlen(str);
int cc = ;
for (i = ; i < len; ++i) {
if (str[i] == ' ') {
++cc;
}
} int tc = ;
for (i = len - ; i >= ; --i) {
if (str[i] == ' ') {
++tc;
} else {
break;
}
}
cc -= tc; j = i;
i = cc;
while (j >= ) {
if (str[j] == ' ') {
--cc;
str[j + * cc] = '%';
str[j + * cc + ] = '';
str[j + * cc + ] = '';
} else {
str[j + * cc] = str[j];
}
--j;
}
if ( * i > tc) {
str[len - tc + * i] = ;
}
}
}; int main()
{
char str[];
Solution sol; while (gets(str) != nullptr) {
sol.replaceSpace(str);
puts(str);
} return ;
}

《Cracking the Coding Interview》——第1章:数组和字符串——题目4的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. 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 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(一)

    1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...

  10. C语言 第七章 数组与字符串

    一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...

随机推荐

  1. IOS 制作动画代码和 设置控件透明度

    方式1: //animateWithDuration用1秒钟的时间,执行代码 [UIView animateWithDuration:1.0 animations:^{ //存放需要执行的动画代码 s ...

  2. eclipse使用maven install 命令,生成war包中没有jsp/js/css的解决方法

    在pom.xml文件中添加如下11行代码就可以了. <build> <plugins> <plugin> <groupId>org.apache.mav ...

  3. PHP设计模式练习——制作简单的投诉页面

    ---恢复内容开始--- <?php /* * 设计模式练习 * 1.数据库连接类(单例模式) * 2.调用接口实现留言本功能(工厂模式) * 3.实现分级举报处理功能(责任链模式) * 4.发 ...

  4. AVR446_Linear speed control of stepper motor步进电机曲线分析

    1.1.  单片机代码处理 // 定义定时器预分频,定时器实际时钟频率为:72MHz/(STEPMOTOR_TIMx_PRESCALER+1) #define STEPMOTOR_TIM_PRESCA ...

  5. ceph-块存储客户端

    ceph块存储 ceph块设备,以前称为RADOS块设备,为客户机提供可靠性.分布式和高性能的块存储磁盘.RADOS块设备利用librbd库并以顺序的形式在ceph集群的多个osd上存储数据块.RBD ...

  6. 阿里数据库连接池druid

    官方wiki: https://github.com/alibaba/druid/wiki 实用方法介绍的想当详细,包含监控.扩展.大力推荐!

  7. link链接外部样式表

    一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...

  8. Android学习<2>

    Android自学资料汇总 资料参考地址: http://blog.csdn.net/guolin_blog/article/details/26365913 http://drakeet.me/an ...

  9. Tinyhttpd 知识点

    1. fork 子进程 #include <stdio.h> #include <unistd.h> int main(void) { pid_t pid; ; pid = f ...

  10. php动态画图

    index.php <?php $width=800; $height=600; //绘图技术 基本步骤 前提:在php.ini文件中启用gd库 //创建画布 默认背景是黑色的 $img=ima ...