【题目描述】

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. Scrapy-多层爬取天堂图片网

    1.根据图片分类对爬取的图片进行分类 开发者选项 --> 找到分类地址         爬取每个分类的地址通过回调函数传入下一层 name = 'sky'start_urls = ['http: ...

  2. java2 - 语言基础

    一.结构流程图制作工具 xmind 的使用 二.Java 基础数据类型 回答以下问题: java 基本数据类型有几种? 整数类型是哪几种? 浮点数类型是哪几种? 字符类型关键字? 布尔类型关键字? 为 ...

  3. Egret学习笔记 (Egret打飞机-6.实现敌机飞起来)

    有了子弹,总得有敌人来打吧,不然游戏有啥意思呢?今天我们来实现敌机从屏幕上边往下飞 参考微信打飞机游戏里面,敌机分为3种 1是特小飞机,2是小飞机,还有一种就是大飞机 面向对象编程提倡抽象,实现代码复 ...

  4. # C# 如何调用动态连接库?

    导入命名空间 using System.Runtime.InteropServices; 把 非托管DLL放入执行程序同级目录,比如 Debug .Release . [DllImport(" ...

  5. POJ - 3087 模拟 [kuangbin带你飞]专题一

    模拟洗牌的过程,合并两堆拍的方式:使先取s2,再取s1:分离成两堆的方式:下面C张放到s1,上面C张到s2.当前牌型与第一次相同时,说明不能搜索到答案. AC代码 #include<cstdio ...

  6. POJ1459 - Power Network

    原题链接 题意简述 原题看了好几遍才看懂- 给出一个个点,条边的有向图.个点中有个源点,个汇点,每个源点和汇点都有流出上限和流入上限.求最大流. 题解 建一个真 · 源点和一个真 · 汇点.真 · 源 ...

  7. MySQL架构篇(一)

    MySQL复制解决了什么问题? 1.实现在不同服务器上的数据分布 2.利用二进制日志增量进行 3.不需要太多的带宽 4.但是使用基于行的复制在进行大批量的更改时会对带宽带来一定的压力,特别是跨IDC环 ...

  8. Luogu P1092 虫食算

    题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045 +8468#6633 44445509678 其中# ...

  9. 笔记+R︱Logistics建模简述(logit值、sigmoid函数)

    本笔记源于CDA-DSC课程,由常国珍老师主讲.该训练营第一期为风控主题,培训内容十分紧凑,非常好,推荐:CDA数据科学家训练营 ---------------------------------- ...

  10. Linux中的DRM

    如果在搜索引擎离搜索 DRM 映入眼帘的尽是Digital Rights Managemen,也就是数字版权加密保护技术.这当然不是我们想要的解释.在类unix世界中还有一个DRM即The Direc ...