Lintcode212 Space Replacement solution 题解
【题目描述】
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索引位重合。
【参考答案】
Lintcode212 Space Replacement solution 题解的更多相关文章
- 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 ...
- Space Replacement
Write a method to replace all spaces in a string with %20. The string is given in a characters array ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解
Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...
- A replacement solution to using Google Drive in Ubuntu
Grive2 Get dependencies You need to get the dependency libraries along with their development (-dev ...
- POJ 2392 Space Elevator 背包题解
多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...
- Lintcode360 Sliding Window Median solution 题解
[题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...
- 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 ...
- 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 ...
随机推荐
- java8 - IO
一.学习大纲: 1. 字符编码格式 2. 文件操作(实现文件的增.删.改.查等操作) 3. 目录操作(实现目录的增.删.改.查等操作) 4. 数据传输(实现对文件内容的读.写等操作) 二.关联类: 1 ...
- JDBC数据库操作
JDBC: 创建SQL语句对象 Statement statement = (Statement) con.createStatement() ; 调用执行 statement. ...
- 深度学习中batch normalization
目录 1 Batch Normalization笔记 1.1 引包 1.2 构建模型: 1.3 构建训练函数 1.4 结论 Batch Normalization笔记 我们将会用MNIST数 ...
- 个人觉得实用的Python姿势
以后会陆续补充 偶然在Python Cookbook看到一个format操作,想到一个问题, 感觉用了!r之后,会把传入的对象按照原来形式保留 d = {"foo": " ...
- python批量修改文件内容及文件编码方式的处理
最近公司在做tfs迁移,后面要用新的ip地址去访问tfs 拉取代码 ,所以原来发布脚本中.bat类型的脚本中的的ip地址需要更换 简单说下我们发布脚本层级目录 :每个服务站点下都会有一个发布脚本 . ...
- ios开发之自定义textView
自定义textView,从理论上讲很简单,根据需求自定义,比如我在开发中的需求就是现实一个字数的限制以及根据输入的文字改变提示剩余字数,那么开始我的基本思路就是自定义一个View,而里面包含一个子控件 ...
- Centos7 上安装配置 RabbitMQ
前言: 最近服务器上搭建了一下rabbitmq, 网上找了很多教程, 采了灰常灰常多的坑. 现在终于从坑里面爬出来了.赶紧写下来,避免下次掉坑里面 好了,废话不多说,下面步入正题 一.安装Erl ...
- 【前端】Angular2 Ionic2 学习记录
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/angular2_ionic2.html 一.建立项目 ionic start ProductName super ...
- Flex弹出窗口请求Action函数
Flex弹出窗口请求Action函数 private function askQuestion(event:MouseEvent):void { var askQuestions:AskWindow ...
- Dshell----开源攻击分析框架
前言 随着互联网的高速发展,网络安全问题变得至关重要,随着网络的不断规模化和复杂化,网络中拒绝服务(Denial of Service,DoS)攻击和分布式拒绝服务(Distributed Denia ...