博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode-easy-Space Replacement
阅读量:4503 次
发布时间:2019-06-08

本文共 1321 字,大约阅读时间需要 4 分钟。

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.

Given "Mr John Smith", length = 13.

The string after replacement should be "Mr%20John%20Smith".

 

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[] string, int length) {        // Write your code here        if(string == null || string.length == 0)            return 0;                int space_count = 0;        int j = length - 1;                while(j >= 0){            if(string[j] == ' ')                space_count++;            j--;        }                int i = length + space_count * 2 - 1;                for(j = length - 1; j >= 0; j--){            if(string[j] == ' '){                string[i--] = '0';                string[i--] = '2';                string[i--] = '%';            }            else{                string[i--] = string[j];            }        }                return length + space_count * 2;    }}

 

转载于:https://www.cnblogs.com/goblinengineer/p/5249287.html

你可能感兴趣的文章
串口通信类,WPF
查看>>
UIView下使用Animation控制动画
查看>>
TP之空操作及View模块
查看>>
shiro学习笔记:授权管理
查看>>
Java 使用正则表达式取出图片地址以及跳转的链接地址,来判断死链(一)
查看>>
代理delegate、NSNotification、KVO在开发中的抉择
查看>>
剑指offer--二叉搜索树的后序遍历序列
查看>>
Selenium学习第一章:搭建测试环境
查看>>
SASS笔记
查看>>
2.学习Application
查看>>
php第二十五节课
查看>>
CS224d lecture 6札记
查看>>
[NOIP 2011]聪明的质监员
查看>>
[Sdoi2013]spring
查看>>
TopCoder SRM 633 Div.2 500 Jumping
查看>>
iOS 相关博客清单
查看>>
GLSL新手上路 -- 《交互式计算机图形学》附录中GLSL代码有误 -- 修改如下
查看>>
xss挑战赛小记 0x02(prompt(1))
查看>>
软件工程 第四课(毕业论文管理系统——面向对象)
查看>>
springboot 获取post请求参数
查看>>