lintcode : 空格替换
题目:
设计一种方法,将一个字符串中的所有空格替换成 %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 : 空格替换的更多相关文章
- C# 中将多个空格替换成一个空格
2013-04-17 15:36 C# 中如何将多个空格替换成一个空格? 1 myString = Regex.Replace(myString, @"\s+", " & ...
- 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”
//字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...
- 剑指offer 1,输入一个字符串,将字符串的空格替换成%20
剑指offer 1,输入一个字符串,将字符串的空格替换成%20 function replaceSpace(str){ return str.replace(/\s/g,"% ...
- 将一个字符串中的空格替换成“%20”(C、Python)
将一个字符串中的空格替换成“%20” C语言: /* ----------------------------------- 通过函数调用,传地址来操作字符串 1.先计算出替换后的字符串的长度 2.从 ...
- shell中空格的使用;空格替换;通配符
测试: test $? -eq && echo "yes" || echo "no" 通配符: 通配符 ()*:0个或多个连续的字符 ()?:任 ...
- linux删除指定行&删除行首空格&替换字符
打印并删除2~1000行 nl /etc/passwd | sed '2,1000d' |more 删除行首空格 sed -i 's/^[][ ]*//g' file 替换分隔符 说明:文件中数据是由 ...
- c# 字符串去掉两端空格,并且将字符串中多个空格替换成一个空格
字符串去掉两端空格,并且将字符串中多个空格替换成一个空格: 主要还是考察使用字符串的方法: trim(); 去掉字符串两端空格 split(); 切割 string.join(); 连接 class ...
- LintCode-212.空格替换
空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序还需要返回被替换后的字符串 ...
- 请实现一个函数,把字符串中的每一个空格替换成“%20”,比如输入 “We are Happly。” 则输出“we%20are%20happy。”
请实现一个函数,把字符串中的每一个空格替换成"%20",比如输入 "We are Happly." 则输出"we%20are%20happy. &q ...
随机推荐
- 【Qt】Qt之进程间通信(QProcess)【转】
简述 前几节里,分享了进程通信的几种方式:Windows消息机制.Shared Memory(共享内存),本节讲解下关于进程通信的另外一种方式-QProcess. 简述 命令行参数启动 说明 实现 命 ...
- 转换 Html 内容为纯文本内容(html,文本互转)
转自http://www.cnblogs.com/jyshi/archive/2011/08/09/2132762.html : /// <summary> /// 转换纯文本内容为 HT ...
- How to modify Code Comments[AX2012]
// This is a framework class. Customizing this class may cause problems with future upgrades to the ...
- springMVC之事务配置(问题来源:为什么数据保存不了)
参考文章:http://www.cnblogs.com/leiOOlei/p/3725911.html 自己的亲身体会,来源问题this.sessionFactory.getCurrentSessio ...
- 使用Azure portal Create Virtual Machine
使用简单快速的方式穿件的Virtual Machine 这个步骤隐藏的了很多步骤,例如的创建的云服务(Cloud Service) 创建存储(Storage) 存储名为系统自动产生 可以通过存储看到含 ...
- MySQL 库大小、表大小、索引大小查询命令
1.进去指定schema 数据库(存放了其他的数据库的信息) mysql> use information_schema; 2.查询所有数据的大小 mysql> sele ...
- 【Node.app】Node.js for iOS
Node.app 是用于 iOS 开发的 Node.js 解释器,它允许最大的代码重用和快速创新,占用资源很少,为您的移动应用程序提供 Node.js 兼容的 JavaScript API.你的客户甚 ...
- Ios拦截手机短信程序
引用 1.手机要越狱,没有越狱的话,下面的可以不用看了! 2.IOS 要5.0以上,4.xx的同上 首先,声明下!由于公司移动开发的项目中,需要根据手机的内容进行逻辑处理,也就是要实现手机短信拦截 ...
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示 ...
- When to use Class.isInstance() & when to use instanceof operator?
I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific ...