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. 2017.10.28 QB模拟赛 —— 下午

    题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...

  2. Selenium入门12 鼠标和键盘事件

    1 鼠标 集成在webdriver.ActionChains.单击.双击.右击.拖放等等.   2 键盘 引入包from selenium.webdriver.common.keys import K ...

  3. 页面文本超出后CSS实现隐藏的方法

    text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hidden !important; dis ...

  4. C. Tanya and Toys_模拟

    C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. jsc

    之前发现一个神器,js代码测试脱离浏览器,mac终端也可以做到 调试js的时候,一般都是用浏览器的开发者工具,这里给大家推荐另外一种,抛开浏览器,在终端执行的方式,Mac内置了一个javascript ...

  6. MapReduce执行jar练习

    1.用程序生成输入文件1.txt和2.txt 生成程序源码如下: https://www.cnblogs.com/jonban/p/10555364.html 2.  上传文件到hdfs文件系统 创建 ...

  7. 关于 npm install 命令

    使用 `npm install` 命令安装模块时 ,有以下几种形式: 安装模块到项目 node_modules 目录下,不会将模块依赖写入 dependencies 或 devDependencies ...

  8. 3.Mysql集群------Mycat分库分表

    前言: 分库分表,在本节里是水平切分,就是多个数据库里包含的表是一模一样的. 只是把字段散列的分到不同的库中. 实践: 1.修改schema.xml 这里是在同一台服务器上建立了4个数据库db1,db ...

  9. TCP心跳的意义

    摘自:https://blog.csdn.net/bjrxyz/article/details/71076442 TCP新手误区–心跳的意义 背景 最近面试了很多的学生,发现很多TCP的新手对于TCP ...

  10. c++标准之IO库

    1.面向对象的标准库 2.多种IO标准库工具 istream,提供输入操作 ostream,提供输出操作 cin:读入标准输入的istream对象.全局对象extern std::istream ci ...