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. C#回调函数的简单讲解与应用例子(最简单讲解,大神绕道)

    本博客一直以来的宗旨就是:用最简单的方式讲清楚不复杂的问题. 因为本人也很菜所以也没法讲太复杂HHHHHH...... 所以如果哪天某个大神看到了觉得讲的有问题欢迎指出. 话不多说进入正题.. ——— ...

  2. 组件 --BreadCrumb--面包屑

    面包屑组件多用于导航栏,对于大型网站,做面包屑导航栏 .breadcrumb .breadcrumb-item .active:表示现在正处在该页面 效果截图: 代码: <nav> < ...

  3. wifi 标准

    简介 https://smb.pconline.com.cn/1149/11491365.html

  4. [转帖] Kubernetes如何使用ReplicationController、Replica Set、Deployment管理Pod ----文章很好 但是还没具体操作实践 也还没记住.

    Kubernetes如何使用ReplicationController.Replica Set.Deployment管理Pod https://blog.csdn.net/yjk13703623757 ...

  5. jenkins 添加 k8s 云

    同事的jenkins 链接自己的 k8s 总是出问题 给出了资料和服务器 进行处理. 同时给出的参考资料:https://blog.csdn.net/diantun00/article/details ...

  6. 开机自启动nginx,php-fpm

    开机自启动nginx,php-fpm(其他服务类似) centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 ...

  7. 一本通1630SuperGCD

    1630:SuperGCD 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 来源:SDOI 2009 Sheng Bill 有着惊人的心算能力,甚至能用大脑计 ...

  8. 自学Aruba1.1-WLAN一些基本常识

    点击返回:自学Aruba之路 自学Aruba1.1-WLAN一些基本常识 1. LAN.WAN.WLAN.WIFI术语 1.1 局域网(Local Area Network,LAN) 是指在某一区域内 ...

  9. 架构师成长之路1.2-多功能系统信息统计工具dstat

    点击返回架构师成长之路 架构师成长之路1.2-多功能系统信息统计工具dstat dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一 ...

  10. 【Revit API】梁的净高分析

    原理就是,先从梁的LocationCurve上取点,然后向板的上表面投影.如果有投影点,再从投影点(板上)向梁的底面投影,这时候如果有投影点的话就能得到距离了. 运用该分析的第一条件是梁是在板的上方, ...