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.

 
Example

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith", you need to change the string in-place and return the new length 17.

分析:

先过一遍,找出string里的空格个数,这样就可以确定新string的总长度,我们可以从length -1 那个地方倒着插入字符。

 public class Solution {
/**
* @param string: An array of Char
* @param length: The true length of the string
* @return: The true length of new string
*/
public int replaceBlank(char[] str, int length) {
if (str == null || str.length == ) return ; int count = ;
for (int i = ; i < str.length; i++) {
if (str[i] == ' ') count++;
} int index = length + * count - ; for (int i = length -; i >= ; i--) {
if (str[i] == ' ') {
str[index--] = '';
str[index--] = '';
str[index--] = '%';
} else {
str[index--] = str[i];
}
} return length + * count; }
}

Space Replacement的更多相关文章

  1. Lintcode212 Space Replacement solution 题解

    [题目描述] Write a method to replace all spaces in a string with%20. The string is given in a characters ...

  2. 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 ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  5. jQuery表单 Ajax向PHP服务端发送文件请求并返回数据

    ImageAjaxUpLoad.htm <!DOCTYPE html> <head> <meta charset='utf-8'> <title>< ...

  6. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  7. jquery 通过ajax 提交表单

    1.需要引入以下两个js文件 <script src="Easyui/jquery-1.7.2.min.js"></script>    <scrip ...

  8. jquery.form.min.js

    /*! * jQuery Form Plugin * version: 3.51.0-2014.06.20 * Requires jQuery v1.5 or later * Copyright (c ...

  9. HDFS面试题

    hadoop节点动态上线下线怎么操作? )节点上线操作: 当要新上线数据节点的时候,需要把数据节点的名字追加在 dfs.hosts 文件中 ()关闭新增节点的防火墙 ()在 NameNode 节点的 ...

随机推荐

  1. beta1

    组长:吴晓晖 过去两天完成了哪些任务: 代码重构进行中,界面,预计两个beta单位完成 展示GitHub当日代码/文档签入记录 接下来的计划 更加人性化的推荐算法 还剩下哪些任务 有哪些困难 有哪些收 ...

  2. ns3的输入输出奥秘(三) Tracing系统

    Tracing系统 (1)ASCII Tracing 还是以myfirst.cc为例子 可以在Simulator::Run()前面加上 AsciiTraceHelper ascii; pointToP ...

  3. Alpha 冲刺十

    团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 完善各自部分 项目描 ...

  4. [转帖]Programmer’s guide to the big tech companies 💻

    Programmer’s guide to the big tech companies

  5. Exchange2010批量删除邮件

    在Exchange2010里若要删除某个用户发出的邮件,可以通过EMC控制台授予管理员“管理完全访问权限”,通过OWA登录到用户邮箱删除.另外,更简便的方法为使用Exchange2010的命令来处理, ...

  6. python基础教程1:入门基础知识

    写在系列前,一点感悟 没有梳理总结的知识毫无价值,只有系统地认真梳理了才能形成自己的知识框架,否则总是陷入断片儿似的学习-遗忘循环中. 学习方法真的比刻苦"傻学"重要多了,而最重要 ...

  7. Semi synchronous replication

    目标 主库宕机不丢数据(Master Failover without data loss) facebook有两篇不错的文章: 2015/01: performance-issues-and-fix ...

  8. 用JS制作博客页面背景随滚动渐变的效果

    今天颓了一会,用JavaScript给我的博客园博客写了一个页面背景随滚动而渐变的效果,做完之后自我感觉良好-- 下面就以我的博客园博客为例,介绍一下如何制作这个效果! 准备 [x] 申请博客园的JS ...

  9. Codechef Sad Pairs——圆方树+虚树+树上差分

    SADPAIRS 删点不连通,点双,圆方树 非割点:没有影响 割点:子树DP一下 有不同颜色,所以建立虚树 在圆方树上dfs时候 如果当前点是割点 1.统计当前颜色虚树上的不连通点对,树形DP即可 2 ...

  10. hiho_offer收割18_题解报告_差第四题

    I.求逆元欧几里得方法 II.模拟细心+耐心 *本人感悟:自己的错误在于:对于这道模拟题没有耐心静下来一字一字看题,一行一行调错,一步一步调试,我要引以为戒. III.dpf[i][j][k]=max ...