【题目描述】

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. vue学习问题总结(一)

    使用comopontent组件报错错误信息:vue.js:491 [Vue warn]: Unknown custom element: <todo-item> - did you reg ...

  2. 树莓派系列教程:1.环境与系统,无显示器无键盘无网线联网并使用PuTTy与VNC图形界面远程登录

    本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...

  3. applicationContext.xml最基本配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 几种优化ajax的执行速度的方法

    1.尽量使用局部的变量,而不使用全局变量: 2.优化for循环 3.尽量少用eval,每次使用eval都需要消耗大量的时间: 4.将DOM节点放在文档上. 5.尽量减少点好(.)操作符号的使用

  5. 使用maven搭建hibernate的pom文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. H3C虚拟化之IRF

    SA system-view irf domain 10 irf member 1 ren 1 y int ten 1/0/50 shu qu irf-port 1/1 port group int ...

  7. 利用linq的Take Skip方法解决 集合拆分的问题

    public static void Main(string[] args) { List<string> list = new List<string>(); ; i < ...

  8. 【Unity3D】Unity3D 摄像机带透明截图

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/CropCamera.html ↓↓↓下面的废话可以不看↓↓↓ 最近处理了一批我的游戏的图标,步骤特别繁琐, 需要先 ...

  9. linux命令之 ifconfig

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  10. 基于busybox的Linux小系统制作 (initrd)

    我们有时候有需要在busybox基础上,制作linux,可是却不知道具体怎么做,这里将对基于busybox的linux小系统制作做出详细的步骤说明.准备环境:1.一个Redhat完整系统的虚拟机,本次 ...