【题目描述】

Write a method to replace all spaces in a string with%20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.

You code should also return the new length of the string after replacement.

Notice:If you are using Java or Python,please use characters array instead of string.

设计一种方法,将一个字符串中的所有空格替换成%20。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。

你的程序还需要返回被替换后的字符串的长度。

【注】如果使用 Java 或 Python, 程序中请用字符数组表示字符串。

【题目链接】

www.lintcode.com/en/problem/space-replacement/

【题目解析】

先统计字符串中含有的空格数A,设原来长度为len,则最终字符串长度为len+A*2。

原问题转换成一个两点移动问题。设一个指针P1指向最终字符串尾部,另一个P2指向原字符串尾部

分析P2字符:

如果不是空格,P2字符复制到P1,P1、P2左移

如果是空格,P1填入三位%20,总供左移了3位,而P2左移1位

按3步骤不断操作,最终两指针一定会在字符串0索引位重合。

【参考答案】

www.jiuzhang.com/solutions/space-replacement/

Lintcode212 Space Replacement solution 题解的更多相关文章

  1. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  2. Space Replacement

    Write a method to replace all spaces in a string with %20. The string is given in a characters array ...

  3. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

    [题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...

  4. CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解

    Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...

  5. A replacement solution to using Google Drive in Ubuntu

    Grive2 Get dependencies You need to get the dependency libraries along with their development (-dev ...

  6. POJ 2392 Space Elevator 背包题解

    多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...

  7. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  8. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  9. Lintcode248 Count of Smaller Number solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...

随机推荐

  1. centos7 yum与Python3冲突

    虽然标题不严谨,但是,我自己尝试了多次,在安装了Python3.6后,yum无法同步Python.所以采用网上抄来抄去的做法,将yum指定使用Python2,凡是需要安装与Python相关的模块,使用 ...

  2. C++ stl 怎么打印内存内容?

    #include <iostream> #include <string> #include <sstream> #include <iomanip> ...

  3. POJ - 1456 贪心+并查集

    做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...

  4. git 命令和使用场景总结

    资料地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000    http://w ...

  5. 使用阿里云主机离线部署CDH步骤详解

    一.Linux文件系统准备 1. 拍摄快照 登录阿里云控制台,拍摄快照,注意有几个关键点尽量拍摄快照,系统初始状态.CM环境准备完成.CM安装完成.CDH安装完成. 2. 挂载设备 三个主机都执行. ...

  6. NLP+VS︱深度学习数据集标注工具、方法摘录,欢迎补充~~

    ~~因为不太会使用opencv.matlab工具,所以在找一些比较简单的工具. . . 一.NLP标注工具BRAT BRAT是一个基于web的文本标注工具,主要用于对文本的结构化标注,用BRAT生成的 ...

  7. 查看dmp文件

    1.查看dmp文件,首先要通过以下的链接地址进行下载 http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.m ...

  8. RAID卡技术简析

    经过一段时间的折腾,工作的事终于解决了,新工作一上来的第一件事就要熟悉RAID卡存储机制,先简单了解下RAID卡吧. 提到RAID卡就不得不提什么是RAID,RAID是英文Redundant Arra ...

  9. python 内置函数range和xrange

    range range 是一种类型(type),它是一个数字的序列,而且是不可变的,通常用在for循环中. class range(stop)class range(start, stop [, st ...

  10. Redis总结(七)Redis运维常用命令

    redis 服务器端命令 redis 127.0.0.1:6380> time  ,显示服务器时间 , 时间戳(秒), 微秒数 1) "1375270361" 2) &quo ...