题目:

设计一种方法,将一个字符串中的所有空格替换成 %20 。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。

样例

对于字符串"Mr John Smith", 长度为 13

替换空格之后的结果为"Mr%20John%20Smith"

注意

如果使用 Java 或 Python, 程序中请用字符数组表示字符串。

解题:

表示做到的通过率最低的一道题。。。。

用java的字符串链接起来,然后toCharArray,然后出去接个和之前的一样,原来%20 表示的是一个空格。。。。。。。参考程序,直接在原char字符数组上操作,先求新的字符串数组的长度,再从后插入%20,这样就解决了,对于从后插入求解的碰到过,题目都会给这样的提示:假设该字符串有足够的空间来加入新的字符,要根据题目找答案,找思路。

Java程序:

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
int reallen = length;
for(int i = 0;i<length;i++){
if(string[i] == ' ')
reallen += 2;
}
int index = reallen;
for(int i = length - 1;i>= 0 ;i-- ){
if(string[i] == ' '){
string[--index] = '0';
string[--index] = '2';
string[--index] = '%';
}else{
string[--index] = string[i];
}
} return reallen;
}
}

总耗时: 1137 ms

Python程序:

class Solution:
# @param {char[]} string: An array of Char
# @param {int} length: The true length of the string
# @return {int} The true length of new string
def replaceBlank(self, string, length):
# Write your code here
if string==None:
return None
reallen = length
for si in string:
if si==' ':
reallen += 2
index = reallen
for i in range(length-1,-1,-1):
if string[i] == ' ':
index -= 1
string[ index ] = ''
index -= 1
string[ index ] = ''
index -= 1
string[ index ] = '%'
else:
index -= 1
string[ index ] = string[i]
return reallen

总耗时: 393 ms

lintcode : 空格替换的更多相关文章

  1. C# 中将多个空格替换成一个空格

    2013-04-17 15:36 C# 中如何将多个空格替换成一个空格? 1 myString = Regex.Replace(myString, @"\s+", " & ...

  2. 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

    //字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...

  3. 剑指offer 1,输入一个字符串,将字符串的空格替换成%20

    剑指offer 1,输入一个字符串,将字符串的空格替换成%20    function replaceSpace(str){      return str.replace(/\s/g,"% ...

  4. 将一个字符串中的空格替换成“%20”(C、Python)

    将一个字符串中的空格替换成“%20” C语言: /* ----------------------------------- 通过函数调用,传地址来操作字符串 1.先计算出替换后的字符串的长度 2.从 ...

  5. shell中空格的使用;空格替换;通配符

    测试: test $? -eq && echo "yes" || echo "no" 通配符: 通配符 ()*:0个或多个连续的字符 ()?:任 ...

  6. linux删除指定行&删除行首空格&替换字符

    打印并删除2~1000行 nl /etc/passwd | sed '2,1000d' |more 删除行首空格 sed -i 's/^[][ ]*//g' file 替换分隔符 说明:文件中数据是由 ...

  7. c# 字符串去掉两端空格,并且将字符串中多个空格替换成一个空格

    字符串去掉两端空格,并且将字符串中多个空格替换成一个空格: 主要还是考察使用字符串的方法: trim(); 去掉字符串两端空格 split(); 切割 string.join(); 连接 class ...

  8. LintCode-212.空格替换

    空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序还需要返回被替换后的字符串 ...

  9. 请实现一个函数,把字符串中的每一个空格替换成“%20”,比如输入 “We are Happly。” 则输出“we%20are%20happy。”

    请实现一个函数,把字符串中的每一个空格替换成"%20",比如输入 "We are Happly."  则输出"we%20are%20happy. &q ...

随机推荐

  1. NSS_11 Server Error in '/' Application

    昨天手贱在Home/Index下点了下鼠标,set as start page,然后程序一直运行不起来, 一度以为mvc的route失效了, 一直报一个错误如下:

  2. yum最常用的命令

    yum是一个用于管理rpm包的后台程序,用python写成,可以非常方便的解决rpm的依赖关系.在建立好yum服务器后,yum客户端可以通过 http.ftp方式获得软件包,并使用方便的命令直接管理. ...

  3. 【Qt】Qt之自定义界面(右下角冒泡)【转】

    简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...

  4. 重拾C,一天一点点_6

    break与continuecontinue只能用于循环语句goto最常见的用法是终止程序在某些深度嵌套的结构中的处理过程,例如一次跳出两层或多层循环.break只能从最内层循环退出到上一级的循环. ...

  5. PHP:strpos()-返回字符串在另一个字符串中第一次出现的位置

    strpos()函数返回字符串在另一个字符串中第一次出现的位置.如果没有找到该字符串,则返回false. 语法:strpos(sting, find [, start]) string ,必须,要搜索 ...

  6. sql拆分查询

    有这样一个需求: 临时表sql: create table #AA ( ID int, Name nvarchar(20) ) insert #AA select 1,'苏州/上海/温州' union ...

  7. COUNT(*),count(1),COUNT(ALL expression),COUNT(DISTINCT expression)

    创建一个测试表 IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; GO )); GO INSERT INT ...

  8. Error:/etc/fstab:Read-only file system错误的解决办法

    1.挂载60T存储,设置开机自动挂载,UUID编号配置错误导致系统无法启动 2.根据提示进入维护状态,输入root密码,进入fstab删除UUID等内容,结果报错     Error:/etc/fst ...

  9. php 彩票类 lottery

    <?php /* * For the full copyright and license information, please view the LICENSE * file that wa ...

  10. 0x04 高级语法

    while-endw .while(条件) 循环体(条件满足时执行) .endw repeat-until .repeat 循环体(条件不满足时执行) .until(条件) if-elseif-end ...